Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added change string functionality in Typing Speed Calculator #825

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}