Mutation issue - is my if statement wrong?

Can someone tell me why my code is not working?


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

}

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

Hi

Your code only tests the first letter of the second string to see if it exists in the other string. Based on the result of that test your function must either return true or return false and then end .

You need to test all letters of second string, failing (returning false) once an unmatched letter is found. If loop runs to completion without failing, only then you can return true.

Your indentation and code style made this a little hard to follow. Iā€™m assuming this is what you were after.

function mutation(arr) {
  var frst = arr[0].toLowerCase();
  var second = arr[1].toLowerCase();
  var scnd = second.split("");

  for (var i = 0; i < scnd.length; i++) {
    console.log(i)
    if (frst.indexOf(scnd[i]) === -1) {
      return false;
    } else {
      return true;
    }
  }
}

Good luck!

2 Likes