Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get elements from an array that do not have certain letter, just using the for loop?

I need to get the elements from an array that do not have the letter 'l' in them and push it to another array, just using the for loop. For this project no special methods are allowed. I can only use the push() and toLowerCase() methods.

The result I get is a list of all the names repeated multiple times.

var list =['sage', 'carolina', 'everly', 'carlos', 'frankie'];
var list1 =[];

for(let i in list){
  for(let j in list[i]){
//Get name without the 'l'
    if(list[i][j].toLowerCase() !== 'l'){
      list1.push(list[i]);
    }
  }
}
console.log(list1);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A few notes to make:

  1. First check whole word if it contains the letter
  2. Outside that loop, push result so it gets only added once
  3. Better to check it has the letter then the reverse 🙂
var list = ['sage', 'carolina', 'everly', 'carlos', 'frankie'];
var result = [];

for(let i in list){
  var hasL = false;
  // Check every letter
  for(let j in list[i]){
    if(list[i][j].toLowerCase() === 'l') {
      hasL = true;
      break;
    }
   }
  
  // If the condition (hasL) hasn't been met, push to result
  if (!hasL) {
    result.push(list[i]);
  }

}

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading