Help with Mutations

Help with Mutations
0

#1

Im in the Mutations problem and I don’t know what Im doing wrong, please help.

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

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

I have green ticks in all of them exept the ° mutation([“hello”, “hey”]) should return false.


#2

I believe your code only checks the first letter of array b, even though you’re looping it. In this example, h from hey (b[0]) is present in hello so it’s returning true. I say “believe” cause I’m not 100% sure. My solution was quite more elaborate.


#3

@Soupedenuit is correct, since you have a return in the first iteration of your loop regardless of the condition being true or false, your loop is returning early


#4

You’re returning a value too early. Think about when you should return false and when to return true.


#5

thank you guys, I have fixed it, now it works :smiley:

  var b=arr[1].toLowerCase();
  var a=arr[0].toLowerCase();
  for (i = 0; i<b.length; i++) {
    if (a.indexOf(b[i]) === -1) {
      return false;
    }
    }
      return true;
}

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