Why is my for loop returning the correct answer, but my forEach does not?

This is my first SO question. I have an assignment where I have to create a function that has two parameters: the first one is an array of strings, and the second one is a string that will possibly match in the array.

I have two variants of the function, one uses a for loop, the other uses the .forEach () method. The for loop will exactly return "true" / "false" depending on whether or not the second parameter exists in the array. ForEach always returns "false".

Can someone explain why? Code below:

.forEach () version:

function insideArray (array, word) {
  var value;
  array.forEach(function(each) {
    if(each === word) {
      value = "true";
      return value;
    }
    else {
      value = "false";
    }
  });
  return value;
}

      

for loop version:

function insideArray (array, word) {
  var value;
  for(var i = 0; i < array.length; i++) {
    if(array[i] === word) {
      value = "true";
      return value;
    }
    else {
      value = "false";
    }
  }
  return value;
}

      

Array example:

var heroArray = [ "spiderman", "wolverine", "batman", "greenArrow", "boosterGold" ];

      

Testing .forEach ():

insideArray(heroArray, "spiderman");
"false"

      

Cycle testing:

insideArray(heroArray, "spiderman");
"true"

      

Thanks for any help in advance!

+3


source to share


3 answers


This is because you are returning from the forEach callback, not from insideArray()

, so it insideArray()

always returns false at the end , except when you are matching the last element of the array. you can fix the code by initializing the value with false and removing the else clause like this:

function insideArray (array, word) {
  var value = "false";
  array.forEach(function(each) {
    if(each === word) {
      value = "true";
    }
  });
  return value;
}

      



Also note that you can write simpler code for this using indexOf

:

function insideArray (array, word) {
  return array.indexOf(word) >= 0 ? "true" : "false";
}

      

+3


source


The method .forEach

returns nothing, so I would change your code to this:



function insideArray (array, word) {
  var value = "false";
  array.forEach(function(each) {
    if(each === word) {
      value = "true";
    }
  });
  return value
};

      

+2


source


You can not break

/ return

of the cycle the Array # forEach , although you could throw

, and catch

from him (but not necessary). Array # some will probably be the best choice if you are going to try this way. A better, albeit slightly less well supported method would be to use Array # includes ( polyfills available or translated from babel ). Another solution would be Array # indexof

function insideArray(array, word) {
  return String(array.some(function(each) {
    return each === word;
  }));
}

var heroArray = ['spiderman', 'wolverine', 'batman', 'greenArrow', 'boosterGold'];

console.log(insideArray(heroArray, 'supergirl'));
console.log(insideArray(heroArray, 'batman'));
      

Run codeHide result


const insideArray = (array, word) => String(array.includes(word));
const heroArray = ['spiderman', 'wolverine', 'batman', 'greenArrow', 'boosterGold'];
console.log(insideArray(heroArray, 'supergirl'));
console.log(insideArray(heroArray, 'batman'));
      

Run codeHide result


+1


source







All Articles