Skip to content

Commit

Permalink
Added change string functionality in Typing Speed Calculator (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anurag-Gour authored May 24, 2024
1 parent 14cea2d commit 38e705f
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions Calculators/Typing-Speed-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
let startTime;
let endTime;
let typedText = '';
let typedText = "";
const sentences = [
'The quick brown fox jumps over the lazy dog',
'The five boxing wizards jump quickly',
'Pack my box with five dozen milk jugs',
'How jumping frogs can level six piqued gymnasts',
'The quick onyx goblin jumps over the lazy dwarf',
"The quick brown fox jumps over the lazy dog",
"The five boxing wizards jump quickly",
"Pack my box with five dozen milk jugs",
"How jumping frogs can level six piqued gymnasts",
"The quick onyx goblin jumps over the lazy dwarf",
];

function getRandomSentence() {
let randomIndex = Math.floor(Math.random() * sentences.length);
document.getElementById('text-to-type').textContent = sentences[randomIndex];
document.getElementById("text-to-type").textContent = sentences[randomIndex];
}

function startTimer() {
Expand All @@ -27,8 +27,8 @@ function checkInput() {
if (!startTime) {
startTimer();
}
typedText = document.getElementById('user-input').value;
let textToType = document.getElementById('text-to-type').textContent;
typedText = document.getElementById("user-input").value;
let textToType = document.getElementById("text-to-type").textContent;
if (typedText === textToType) {
stopTimer();
}
Expand All @@ -44,16 +44,24 @@ function calculateSpeed() {
}

function displaySpeed(wordsPerSec, charactersPerSec) {
let resultElement = document.getElementById('speed-result');
resultElement.innerHTML = `Your typing speed is: i) ${wordsPerSec.toFixed(2)} words/sec
let resultElement = document.getElementById("speed-result");
resultElement.innerHTML = `Your typing speed is: i) ${wordsPerSec.toFixed(
2
)} words/sec
ii) ${charactersPerSec.toFixed(2)} characters/sec.`;
setTimeout(changeString, 1500);
}

function changeString() {
getRandomSentence();
resetInput();
}

function resetInput() {
document.getElementById('user-input').value = '';
document.getElementById('speed-result').innerHTML = '';
document.getElementById("user-input").value = "";
document.getElementById("speed-result").innerHTML = "";
startTime = null;
endTime = null;
typedText = '';
typedText = "";
getRandomSentence();
}
}

0 comments on commit 38e705f

Please sign in to comment.