Problem:
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);
Solution:
A few notes to make:
- First check whole word if it contains the letter
- Outside that loop, push result so it gets only added once
- 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);