DEV Community


Discussion on: Interview question for a Senior Js Developer, pt 1 (Questions), updated with answers link.

parkadzedev profile image
Michael Parkadze

Correct me if I am wrong but for the first question the answer is to change the condition to !== -1 as indexOf returns the starting point of the substring that was found.

So I think because ‘super is awesome’ returns an error is because it returns a 0 and it might not consider it in the condition to be true.

eulier profile image
scottbromander profile image
Scott Bromander

You are correct. The case breaks because superman in the second case starts at the first position, array index 0.

You would have to do some other shifting as well to make a !== work. !str.toLowerCase().indexOf('superman') is technically false. So false !== -1 is actually true. But false !== 7 is also true. A good way to approach it if you wanted to use equality would be something like str.toLowerCase().indexOf('superman') === -1 ) for the condition.

But generally, I favor less characters, so something like:
if (str.toLowerCase().indexOf('superman') < 0).

But, as always, shred my solution! I love feedback. Only way to grow is to learn from others who are more right!