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.
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.
1 Like
@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
2 Likes
You’re returning a value too early. Think about when you should return false and when to return true.
1 Like
thank you guys, I have fixed it, now it works
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"]);
2 Likes