here is the question
- Filter out companies which have more than one 'o' without the filter method
- 0: "Facebook" 1: "Google" 2: "Microsoft" 3: "Apple" 4: "IBM" 5: "Oracle" 6: "Amazon"
CodePudding user response:
u need to loop through the array and verifie if each word has more than one "o"
her is a function to verifie the word:
const verif=(word)=>{
var s=0
var l=word.length
for(let i=0 ; i<=l;i ){
if(word[i]=="o"){
s
}
}if(s>=2){
return true
}else{
return false
}
}
CodePudding user response:
You can create a function a iterate through each array element
function filterArray(arr, fn) {
let filteredArray = [];
for (let i = 0; i < arr.length; i) {
if (fn(arr[i]) === true) {
filteredArray.push(arr[i]);
}
}
return filteredArray;
}
function isIsogram (str) {
return !/(.).*\1/.test(str);
}
const arr = ["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle", "Amazon"];
console.log(filterArray(arr, isIsogram));
CodePudding user response:
You could write a loop.
const values = [
"Facebook", "Google", "Microsoft",
"Apple", "IBM", "Oracle", "Amazon"
];
const result = [];
for (const value of values) {
if ((value.match(/o/g) || []).length > 1) result.push(value);
}
console.log(result);
You could use reduce().
const values = [
"Facebook", "Google", "Microsoft",
"Apple", "IBM", "Oracle", "Amazon"
];
const result = values.reduce((acc, val) => {
if((val.match(/o/g) || []).length > 1) acc.push(val);
return acc;
}, []);
console.log(result);
But it is better to stick with filter().
const values = [
"Facebook", "Google", "Microsoft",
"Apple", "IBM", "Oracle", "Amazon"
];
const result = values.filter(v => (v.match(/o/g) || []).length > 1);
console.log(result);
