Confusing in Mutations Algorithm

Hey guys, I think am confused about the way for loop and forEach works in regards to looping. In my mind, regarding the Mutations problems, the solution uses the for loop but I used the forEach loop thinking it would do the same thing but seems like am wrong. Can someone explain to me why my implementation of forEach is wrong.

function mutation(arr) {
  let test = arr[1].toLowerCase().split('');
  let target = arr[0].toLowerCase().split('');

  test.forEach( e => {
    if (target.indexOf(e) < 0) return false;
  return true;

I do not remember exactly the challenge, anyway the significant dfference between the two methods here is that for loop can be stopped, forEach can’t.
forEach executes your callback once for each element, no matter what^^

1 Like

Oh so it will go on for every element, regardless of the return statement in each iteration?

You are correct! :+1:

Thanks a lot!, I brought for loop thinking into a higher order function fight. :rofl:

1 Like

return in the forEach() method is inside callback function and related only to that callback.

1 Like