vendredi 18 juin 2021

For loop won't increment

I want to find the words in an array that contain certain characters. However if I try to find them with a for loop, it only iterates once. It should give me the first and the last item from the array below.

arr = ["knighthood", "parental", "fridge", "clingfilm"]

function longest7SegmentWord(arr) {
  newarr = []
  for (i = 0; i < arr.length; i++) {
    set = new Set(arr[i].split(""))
    if (set.has("k" || "m" || "v" || "w" || "x") == true) {
      newarr.push(arr[i])

    }
  }
  return newarr
}
longest7SegmentWord(arr)["knighthood"]

But the code returns only an array with one element.

Aucun commentaire:

Enregistrer un commentaire