.indexOf refuses to stop confusing me


#1

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


#2
 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;
   
     }else{ 
     
       return false; 
    
    }
    }    
    } 
    
   
 
mutation(["hello", "hey"]); ```

#3

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.


#4

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


#5

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


#6

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


#7

.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.

HTH


#8

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…


#9

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.


#10

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


#11

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.


#12

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


#13

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.


#14

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


#15

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”] )


#16

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


#17

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.


#18

Yes with the ternary operator:

condition ? expr1 : expr2 

#19

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


#20

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: