[Solved] Wrong output from a while loop using indexOf [duplicate]


So now the problem is that .indexOf still returns the true index of “ea” in wordTwo – it doesn’t take into account where you start from. Also, setting positionCount equal to where you find the word and then searching from that position again is just going to make you immediately find the same instance of that word, not the next one.

The index of the first instance of “ea” in wordTwo is 18, so wordTwoLength will be set to 32-18, or 14. Then you’ll find the same instance of ea in wordTwo, and wordTwoLength will be set to 14-18, or -4. Then you’ll exit the while loop, with appearances being 2.

1

solved Wrong output from a while loop using indexOf [duplicate]