Tell us what’s happening:
the code is not passing the tests for:
mutation([“hello”, “Hello”]) and mutation([“floor”, “for”])
i really want to use only for loops, and not the indexOf function.
Your code so far
function mutation(arr) {
let indexer = 0;
let first = arr[0].toLowerCase();
let second = arr[1].toLowerCase();
for (let i = 0; i < second.length; i++){
for(let j = 0; j < first.length; j++){
if(second[i] === first[j]) indexer++;
}
}
return (indexer == second.length);
}
mutation(["hello", "hey"]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Your indexer will increment 4 times here:
1 for letter f, 1 for letter r - that’s all good
but for letter o there will be 2 incrementations, because this letter occurs 2 times in the floor word.
4 is not equal 3(length of for word), so your function will return false
Similar story with
here also will be extra incrementations, andindexer will be 7 instead of 5
You can get rid of the indexer variable and in the final return statement simply return true because if you make it all the way through the nest for loops, you never returned false, so you know all the letters were found. You really do not need the last variable either. You can remove it and all logic associated with it and still pass.