Tell us what’s happening:
my code works for every condition except for mutation([“hello”, “hey”])… i returns true. How can that be ? anyone know what I’m doing wrong?? Your code so far
function mutation(arr) {
var elem1=arr[0].toLowerCase().split('');
var elem2=arr[1].toLowerCase().split('');
for(var i=0;i<elem2.length;i++){
if (elem1.includes(elem2[i])){
return true
}
else return false
}
return arr;
}
console.log(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/88.0.4324.150 Safari/537.36.
When facing the issue of “This seems right, but it doesn’t work”, or actually any other problem, I always recommend to:
Double check for potential spelling errors, or similar
Use console.log() at different points, to find out where the error might be
Go through the code yourself with an example.
After doing these three things, I will soon post the answer here
Answer:
Everything seems correct, as far as spelling goes.
The second step helped me find the solution:
If you log elem2[i] within the if statement it only logs “h” to the console. The mistake is, that at the end of the if statement, you have a return, which is why the for loop doesn’t get executed again.