Mutations getting all but the first challenge right!

This code passes all but the ‘hello’, ‘hey’ challenge. It looks like it’s running the first letter or two. I can fix it but I’m having a hard time figuring out how to see what it’s doing.

function mutation(arr) {
var words = arr;
var firstString = words[0].toString().toLowerCase();
var secondString = words[1].toString().toLowerCase();

for (var i = 0; i <= secondString.length; i++) {
if (firstString.indexOf(secondString[i]) !== -1) {
return true;

}

else {
  return false;
}

}

}
mutation([“hello”, “hey”]);

1 Like

Got it fixed.

SPOILER ALERT*******

function mutation(arr) {
var words = arr;
var firstWord = words[0].toString().toLowerCase();
var secondWord = words[1].toString().toLowerCase();
var tester = secondWord.length;

for (var i = 0; i < secondWord.length; i++) {
if (firstWord.indexOf(secondWord[i]) !== -1) {
tester–;
}
}

if (tester === 0) {
return true;
}
else {
return false;
}

}

mutation([“hello”, “hey”]);

1 Like

I am having the same problem here, only the first one wont pass :frowning: and i cant see to figure it out.

`function mutation(arr) {
var firstStrLower = arr[0].toLowerCase();
var secondArrLower = arr[1].toLowerCase().split("");

 var i = 0;
 while(firstStrLower.indexOf(secondArrLower[i]) !== -1 ) {
   i++;
   return true;
 } return false; 

}

mutation([“hello”, “hey”]);
`

[Solved]
I have the same problem and it doesn’t make sense to me how every other test passes.


function mutation(arr) {
  
  control = arr[0].toLowerCase();
  test = arr[1].toLowerCase().split('');
  
  
  for (i = 0; i < test.length; i++){
     if(control.indexOf(test[i]) <  0){
       return false;
     }
    return true ;
  }
  
}

mutation(["hello", "hey"]);

I don’t understand how your countdown var tester = secondWord.length; solves the problem.

EDIT: Ok wow I got it, I just needed to put the return true outside of the scope of the for loop.

1 Like

[Solved]
This is working::

function mutation(arr) {
var args0=arr[0].toLowerCase();
var args1=arr[1].toLowerCase();
for(var i=0;i<args1.length;i++)
{
if(args0.indexOf(args1[i])===-1){return false;}
}
if (i===args1.length) return true;

}

mutation([“hello”, “hey”]);

1 Like

I have the same problem with the first one not passing, here is my code:

function mutation(arr) {
  for (var i = 0; i < arr[1].length; i++) {
    if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== -1) {
      return true;
    } else return false;
  }
}

mutation(["hello", "hey"]);

and I get true on the first one:


mutation(["hello", "hey"]);
true

Hey,
I had the same exact problem. Why should we put the “return true” statement oustide of the loop function? Shouldn’t it be inside as an “else” statement?

It seems as if we do it as we did, it loops only the first word of the second word, and returns therefore true for all the challenges, apart from the first one, where the missing letter is at the end (“hey”).
Mysterious! Could someone explain this? :smiley: