.indexOf refuses to stop confusing me

I’m having a very hard time understanding how .indexOf works. I’ve read the MDN page on it,but it’s still foggy, if anyone can clarify my code, that would help a lot.
Thank you

 var first = arr[0].toLowerCase();
 var second = arr[1].toLowerCase();
  for ( var i = 0; i <= second.length; i++ ) {
    if ( first.indexOf(second[i]) !== -1 ) {
     return true;
       return false; 
mutation(["hello", "hey"]); ```

This isn’t really about indexOf, but you can shorten this code by getting condensing the if else statement to one line. The condition inside the if statement will evaluate to either a true or false, so all you have to do say:

return first.indexOf(second[i]) ! == -1;

So what are you trying to do with this function? Using return like that inside of the for loop kind of ruins the purpose of a for loop because it won’t even get to the second character in the string. The indexOf function just returns the index number of the argument in whatever string or array you call it on. If the argument doesn’t exist, it will return a -1.

I’ve tried that, it still won’t work. I’m pretty sure I’m missing something about how .indexOf gives results.

Thanks though, I didn’t catch that if loop thing. That’s good info.

What are you trying to do with this function? Is it supposed to match characters in the 2 strings?

.indexOf is pretty cool! It acts as an iterator. It takes a string and iterates over each letter comparing it to the character you put in the parentheses. If it finds a match, then it will return the index of the match, otherwise it will return -1.


I was doing it before where it just compare the whole second word against the first, then with this i’m trying to run through each letter of the second word, so It’ll go through each letter until it hits false or gets to the end and returns true. After looking at it, I understand your confusion lol, too long on one problem with no progress and i start to get a little cloudy…

Thank you, that’s a little more understandable. I can get it to work on all but the example posted, I can’t figure out why, not a clue…

Also, would you be able to tell me how the backticks work? people keep getting mad at me for my posts.

If I do it with one line ( return first.indexOf… ) won’t it only check one letter and then return true?

Ohh sorry I should have been more clear with that. I meant the general pattern of saying,

if (condition) {
      return true;
else {
      return false;

Can be replaced with that one line logic since the condition will either be true or false.

So I am still trying to understand what the purpose of your function is. Are you trying to see if the two words match?

As for the back ticks, I am not sure. It has been a while since I’ve posted code on here and I’m on my phone right now, so hard to look up.

You need to wrap your code with 3 backticks, 3 before, 3 after.

Right lol, sorry guys, I’m a little worn out I think.
I have to see if arr[0] contains all the letters that arr[1] has.
( [“happy”, “hippy”] )

1 Like

that should return false, because there’s no “i” in “happy”.

1 Like

Ahh ok. Instead of using return, you can create a variable with a boolean value equal to true. While looping through the second string, if a character doesn’t match, set the variable to false, and break out of the loop with the keyword: break.
Then outside of the loop you can say

return booleanVariable;

Something like that.


Yes with the ternary operator:

condition ? expr1 : expr2 
1 Like

( oooh ) :hushed: I’ve been looking for something like that since i started trying to learn this stuff.
Nice, nice.


It’s a good shortcut if you just need to test one thing and return true or false. It’s a bit different than if, else statements so make sure to read up so you can use it properly :slight_smile: