'how to filter array items in js without filter method?

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"


Solution 1:[1]

You need to loop through the array and check if each word has more than one "o".

Here is a function to verify 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
  }
}

Solution 2:[2]

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));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mario Petrovic
Solution 2 iamimran