-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypingMaster.js
79 lines (68 loc) · 2.64 KB
/
typingMaster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const setofWords = ["Nothing is impossible. The word itself says ‘I’m possible!",
"There is nothing impossible to they who will try",
"The bad news is time flies. The good news is you’re the pilot.",
"Life has got all those twists and turns. You’ve got to hold on tight and off you go.",
"Keep your face always toward the sunshine, and shadows will fall behind you.",
"Success is not final, failure is not fatal: it is the courage to continue that counts.",
"You define your own life. Don’t let other people write your script."];
const msg = document.getElementById("msgs");
const msgs = document.getElementById("msg");
const typeWords = document.getElementById("myWords");
const btn = document.getElementById("btn");
let startTime, endTime;
//On click of button the function will happend;
btn.addEventListener("click", function () {
if (this.innerText == "Start") {
typeWords.disabled = false;
playgame();
} else if (this.innerText == "Done") {
typeWords.disabled = true;
btn.innerText = "Start";
endGame();
}
})
//if button shows"Start" then playgame function work;
const playgame = () => {
typeWords.placeholder = 'Enter the Message';
let randomNumber = Math.floor(Math.random() * setofWords.length);
msg.innerText = setofWords[randomNumber];
let date = new Date();
startTime = date.getTime();
btn.innerText = "Done";
}
//if button shows"Done" then Endgame function work;
const endGame = () => {
//time count
let date = new Date();
endTime = date.getTime();
let totalTime = ((endTime - startTime) / 1000);
//word count
let totalStr = typeWords.value;
let wordcount = wordCounter(totalStr);
//count speed of typing
let WordSpeed = Math.floor((wordcount / totalTime) * 60);
//display msg of speed.
let finalMSg = "Your Typing Speed is (" + WordSpeed + " Words/min)";
//call function "compareWords" and add into final text.
finalMSg += compareWords(msg.innerText, totalStr);
msgs.innerText = finalMSg;
typeWords.textContent =" ";
}
//call function for word count.
const wordCounter = (str) => {
let responce = str.split(" ").length;
// console.log(responce);
return responce;
}
const compareWords = (str1, str2) => {
let words1 = str1.split(" ");
let words2 = str2.split(" ");
let cnt = 0;
words1.forEach(function (item, index) {
if (item == words2[index]) {
cnt++;
}
});
let errorWords = (words1.length - cnt);
return (". " + cnt + " Words correct out of " + words1.length + " words and the total number of the error are " + errorWords + ".")
}