Why != -1 is not giving the desired result

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

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

console.log(mutation(["hello", "hey"]));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Mutations

Link to the challenge:

You’re almost there. I was able to make a few changes and get your code to pass.

I question your logic. If you find any character that is found in the first string, then you return true? In the example you have, it finds “h” (the first char to check) in “hello” and it instantly declares success and returns true? So, the only way it can return false is if none of the chars match? That is not the logic you want.

2 Likes

Ok so basically, the logic should be to firstly pass all the fail cases (which can be possible when it is <0 / ===-1 ) in the for loop and then if there is no such fail case, then it should return true. I have just started learning, so this may sound very naive to you, but after understanding the flaw in my earlier applied logic (which i hope i am correct about), it amazes me how the order of fail and success in a loop can give you the desired result.

1 Like

Dude, not … at … all. We’ve all been there. We all remember having to figure this stuff out. There is no developer that did not struggle with some of this stuff. Seriously, cut yourself some slack. This is hard stuff, a new way of thinking.

Yes, sometimes the order can have a big effect. Sometimes just inverting the logic improves or even fixes things. You basically had the right idea, you were just doing it kind of backwards.

It sounds like you’re on the right track. If you get stuck again, check back and we’ll see if you can give you a better hint.

1 Like

Thanks a lot mate. I could figure out the logic after your guidance and it worked perfectly fine. I think i am enjoying it all here. Finding a puzzle, then working out on its logic and seeing it working is one of the best feelings. Makes you wanna go for more!

Thanks to the freeCodeCamp community (Big thanks to you) for such a prompt response and encouraging words.

1 Like

Yeah, it’s a great feeling solving an algorithm. Good job. Like I said, you were most of the way there. Have fun on the next one.

And remember two things:

  1. Don’t get discouraged - some of these get hard. A lot of doing algorithms (imho) is building up a knowledge of solutions - when you see something similar in the future, it will be much easier. And it also helps slowly build your “algorithm brain”, getting easier and easier to do these as time goes on.
  2. Don’t be afraid to ask for help. That’s why we’re here. Make a good faith effort, do some research, but if you get stuck, reach out to your friends here at FCC.
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.