Link to challenge:
Solution:
function mutation(arr) {
var firstWord = arr[0].toLowerCase();
var secondWord = arr[1].toLowerCase();
for (var i = 0; i < secondWord.length; i++) {
if (firstWord.indexOf(secondWord[i]) === -1)
return false
}
return true;
}
mutation(["hello", "hey"]);
My confusion is specifically on this line of code:
if (firstWord.indexOf(secondWord[i]) === -1)
I understand that secondWord[i]
loops through each letter of hey
. And the general function of indexOf
is to return the position of the occurrence of the value. But what role does indexOf
play here?