Why my Mutations code doesn't work?

Why my Mutations code doesn't work?
0.0 0

#1

Tell us what’s happening:
Whats wrong with my code? I literally have no idea? Can somebody help?

Your code so far

function mutation(arr) {
  var first = arr[0].toLowerCase();
  var second = arr[1].toLowerCase();
  for(var i = 0; i<second.length; i++){
    if(first.indexOf(second[i]) === -1){
      return false;
    }else{
      return true;
    }
  }
}

mutation(["hello", "hey"]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:


#2

Because your for loop only makes it through the first letter of arr, before you return true or false. Once a return statement is executed, the function is immediately exited regardless if a loop was not finished.


#3

But it works for every single statement but first one.


#4

Pure coincidence. Put a console.log('i = ’ + i) on the first line of your for loop and run the tests and check the console (Ctrl+Shft+J in Chome) and you will only see ‘i = 0’.


#5

Thanks a lot for your reply. I found my mistake and understand.