Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

How to add a word per minute calculator to a website using Javascript, html, or css?

Iv'e been trying to complete a typing test website for some time and am stuck on how to add a word per minute counter. I have tried multiple ways but none of them work. What I want it do do is give the words per minute after the user types a prompt correctly. Code is below:

HTML:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<h1>
   <span id="word-1">man</span> <span id="word-2">become</span> <span id="word-3">as</span>
   <span id="word-4">and</span> <span id="word-5">through</span> <span id="word-6">find</span> <span id="word-7">would</span> <span id="word-8">here</span> <span id="word-9">and</span> <span id="word-10">before</span>
</h1>

<input type="text" id="boch">

        </div>
        <div id="typing-area">

      <button id="bocho" onclick="document.getElementById('boch').value = ''">Enter</button>

</html>
<script src="main.js"></script>

JavaScript:

var input = document.getElementById("boch");
input.addEventListener("keyup", function (event) {
  if (event.keyCode === 13) {
    event.preventDefault();
    document.getElementById("bocho").click();
  }
});


var element = document.querySelector("#boch");

element.onkeyup = function () {
  var value = element.value;

  if (value.includes("man")) {
    document.getElementById('word-1').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-1').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become")) {
    document.getElementById('word-2').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-2').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as")) {
    document.getElementById('word-3').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-3').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and")) {
    document.getElementById('word-4').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-4').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through")) {
    document.getElementById('word-5').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-5').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through find")) {
    document.getElementById('word-6').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-6').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through find would")) {
    document.getElementById('word-7').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-7').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through find would here")) {
    document.getElementById('word-8').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-8').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through find would here and")) {
    document.getElementById('word-9').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-9').style.backgroundColor = 'transparent';
  }

  if (value.includes("man become as and through find would here and before")) {
    document.getElementById('word-10').style.backgroundColor = 'green';
  } else {
    document.getElementById('word-10').style.backgroundColor = 'transparent';
  }

 

}
let tagArr = document.getElementsByTagName("input");
for (let i = 0; i < tagArr.length; i++) {
  tagArr[i].autocomplete = 'off';
}

Thank you for the help!

Irfan

question from:https://stackoverflow.com/questions/65854300/how-to-add-a-word-per-minute-calculator-to-a-website-using-javascript-html-or

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try to store the current timestamp in a variable when a key is pressed for the first time. Then, when the user have finish, get the difference between the current timestamp and the timestamp stored at the start. Next, you have to calculate the elapsed time and convert it to words per minute. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

To avoid your multiple if-else structures, you can split the string by spaces, then compare word by word in a for loop.

const string = "man become as and through find would here and before";

var text = document.getElementById("text");
var input = document.getElementById("input");

var start = null;
var finished = false;

// Split the string to an array of words
var words = string.split(" ");

text.innerHTML = "";
// For each word, add it as a span element in the text
words.forEach(w => {
    let el = document.createElement("span");
    el.innerText = w;
    text.appendChild(el);
    text.innerHTML += " ";
});

input.addEventListener("keyup", () => {
    // Split the input to an array of words
    let input_words = input.value.split(" ");
    let good = true;

    if (finished)
        return;
    if (!start)
        start = Date.now();

    // Iterate over all the words
    for (let i = 0; i < words.length; i++) {
        let element = words[i];
        // If the word is not the same as the word at same position in the input, set 'good' at false
        if (input_words[i] != element)
            good = false;
        // Set the background color for the word, selecting the element
        // by the parent's children index (corresponding to i) to avoid the usage of ids
        text.children[i].style.backgroundColor = good ? "green" : null;
    }
    // If all words are correct, the variable is still true
    if (good)
    {
        finished = true;
        // Get the difference in seonds between start and now
        let elapsed = (Date.now() - start) / 1000;
        // Get words per seconde
        let words_per_seconde = words.length * 60 / elapsed;
        // Round to the fourth decimal
        words_per_seconde = Math.round(words_per_seconde * 1000) / 1000;
        alert(words_per_seconde + " words per minute.");
    }
});
<h1 id="text"></h1>
<input type="text" autocomplete="off" id="input"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...