Tell us what’s happening:
Your code so far
function mutation(arr) {
var string1 = arr[0].toLowerCase().split('');
var string2 = arr[1].toLowerCase().split('');
for(i=0; i<string1.length; i++){
if(string1.indexOf(string2[i]) !== -1){
return true;
}
return false;
}
}
mutation(["hello", "hey"]);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
.
Link to the challenge:
Do you have a specific question?
Yeah. Every condition except the first passes. Why?
Partly, because you only iterate through the string1 array, before you return either true or false. Once the return statement is executed, the function is immediately exited even if it is not finished iterating through the array. You need to modify your logic just slightly for it to work.
An extra hint, you should be iterating through string2 and checking if the string2’s current letter is in string1 and not the other way around.
Thanks! I figured it out with your help!
Why are we setting string1.indexOf(string2[i]) !== -1? This doesn’t make sense to me and yet I know it’s probably the key to why I can’t figure this thing out. What concept am I missing here?
This is a comparison and not an assignment. indexOf returns an value representing the index at which the argument passed to it exists in string1. If -1 is returned instead, that means the argument passed to it does not exist in string1. The *!== is checking if the value on the left side of the !== is not equal to the value on the right side of the !==. If string1.indexOf(string2[i]) returns any value that is not -1, then you know that the character string2[i] exists in string1.