Strings\words comparisons - advice needed

Hello everyone,

I need help to conceptually understand how to compare the following case:


  • Paragraph is given: “Hello Ladies and gentlemen, welcome to my example” .
  • User has to write the same sentence through input bar.

The first word the user types counts until user presses “Spacebar”.
Meaning, if user types Hellooooowe and hits space, the whole words is compared to the first word of the paragraph, i.e “Hello”.
Same for the rest, doesn’t matter how long the words is, it’s still compared to the next of order from pargarph.

My thoughts:

  • To store both the given text and user’s values in two arrays.
  • Whenever user press Space, the word he typed is added to array and compared with the text’s array. like
    userArray[2] === textArray[2].

There is something I can’t understand though,
whenever user hits “backspace” - he can edit the previous word. how?
Copy & Splice the last word from array?
and if user deletes the word as well, he can access the word before that as well.

Sorry for overly complex text

I am not quite sure what you are trying to accomplish, but why not just compare the whole string instead of each word in the string?

And when the word does not match, what do you want to happen? An alert pop up or something? Like @camelcamper asked, what is the ultimate purpose what you want to do? Are you just trying to make sure as a user types in the input box, that the text typed matches up to that point in the paragraph? If so, then after every key stroke in the text box just compare the input’s value to a slice of the paragraph up to the same number of characters currently in the input.

oh yeah, this is the stuff right here

An example of what I mean is:

<p id="para">Hello my name is Randell</p>
<input id="typed" type="text" onKeyUp="checkForMatch()"><br>
<p id="msg"></p>

  function checkForMatch() {
    var paraVal = para.innerText;
    var typedVal = typed.value;
    msg.innerText = typedVal !== paraVal.slice(0, typedVal.length) ? 'Does not match!' : '';

I built a simple typing game with React that evaluates user’s WPM( Words Per Minute), and to make it more accurate I’d like to see when a user types a word and the word doesn’t match the on from the paragraph.

Additionally I want the user to be able to go back (backspace) and fix/edit what he wrote.

I’m essentially trying to imitate this behavior :

try the test and see