[SOLVED] Not understanding why this isn't working from Mutation excercise

Not sure why my code isn’t working for

mutation([“hello”, “hey”]) should return false.

Currently, it’s returning true, while all of the rest of the scenarios are working properly. Here is my code:

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

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

if I do a console.log if temp in the loop, I do see it gets -1, so shouldn’t that be returning false?

You should move the return true outside of the for loop

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

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

So if the condition is not meet the loop continues.

1 Like

Thanks, that did the trick! I looked at that at one point, but my eyes were deceiving me, I thought I was outside of the loop, oops!