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^^