"Mutations" Help

Hi all,

I’ve been trying out different solutions to this challenge and I can’t figure out why this one doesn’t work.

It’s not catching the “y” in “hey” and I’m not sure “y” (ba dum tssh)…

It’s because in the first iteration of for loop it returns value true, so the program ends there(on return statement function will will return the value at that point to the line where it was called).
So you have to return true only when you check all the letters of arr[1] and it doesn’t returned false for any.Hope it helps :slight_smile:

1 Like

Thanks for the reply.

I’m still wondering how to make sure the for loop iterates through all values.

In the below example, the first iteration matches “h” and returns true…

But if I change the if statement (and reverse the returns), it works…

Can someone walk me through why this happens?

for (var i = 0; i < w2.length; i++) { // I'm just iteratin' over w2...
  if (w1.indexOf(w2[i] >= 0) { 
    // Yay, the character in w2[i] also occurs in w1!
    return true; 
    // I guess that means I don't have to check the others, 
    // time to return from the function
  }
  // oh wait, actually I am absolutely wrong. I can only 
  // return true after I made sure EVERY character 
  // in w2 also occurs in w1, not just one.
}
return false; 
// now the loop is done and I can proudly proclaim: No character in w2 
// occurs in w1. This doesn't really help me, though, because it would have 
// been enough to just find ONE such character to prove that not all letters
// occur in w1.

So what I’m trying to say is: you should return true after checking all elements in w2 and return false as soon as you find an element not in w1. You did it the exact opposite way :slight_smile: I hope you don’t find my comments mean, it is all meant in good humor :wink: I still love to make these logical mistakes every now and then, its fun to realize many hours later that even with that heavy thing on my neck, I can’t avoid making silly mistakes.

1 Like

Use array.every()
It will return true if all are present and false otherwise. I’ll leave you to figure out how to use it.

2 Likes