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.
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!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
You can check my answers here dev.to/eulier/interview-question-f....
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 technicallyfalse
. Sofalse !== -1
is actuallytrue
. Butfalse !== 7
is alsotrue
. A good way to approach it if you wanted to use equality would be something likestr.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!