Hi All,
Been working on the mutations problem and it’s whipping me…
Can’t see why this code is not working:
function mutation(arr) {
let word1 = arr[1].toLowerCase()
let word2 = arr[0].toLowerCase()
for (let i = 0; i < word1.length; i++) {
if (word2.indexOf(word1[i])<0) {
return false;
}
return true;
}
}
mutation(["hello", "Hello"]);
It seems to be failing on the indexOf check - for some reason it’s not falling into the return false? For the life of me can’t see the issue, anyone?
Can I get a link to the problem? I can’t remember what you are supposed to be doing.
Hey there. It looks like it doesn’t loop through all of word1[i]. You can check by console logging. It stops at the first letter of word1 and returns the result only of the first letter. I think you shouldn’t return anything yet to be able to loop through all of the letters.
function mutation(arr) {
let word1 = arr[1].toLowerCase()
let word2 = arr[0].toLowerCase()
for (let i = 0; i < word1.length; i++) {
console.log(word1[i]);
if (word2.indexOf(word1[i])<0) {
return false;
}
return true;
}
}
Take return true out of the for loop.
Any time a function encounters a return
it ends the function and returns what is to the right of the return
. Having the return true
inside the loop means it just loops once and when the x < 0
check is false, it ends and returns true.