Typing speed and accuracy are essential skills in today’s digital age. In this tutorial, we will walk you through the process of creating a Typing Speed Test program using JavaScript. By the end, you will have a program that measures and displays the user’s typing speed and accuracy.
Prerequisites:
To follow along with this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript. Familiarity with event handling and DOM manipulation in JavaScript will be beneficial.
Setting Up the HTML Structure:
Let’s start by setting up the HTML structure for our typing speed test program. Create a new HTML file and add the following code:
[code]
<!DOCTYPE html>
<html>
<head>
<title>Typing Speed Test</title>
<link rel=”stylesheet” type=”text/css” href=”styles.css”>
</head>
<body>
<h1>Typing Speed Test</h1>
<p id=”quote”>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<textarea id=”input” placeholder=”Start typing here…”></textarea>
<button id=”startButton”>Start</button>
<p id=”result”></p>
<script src=”script.js”></script>
</body>
</html>
[/code]
In the above code, we have an `<h1>` heading for the title, a `<p>` element to display a quote for the user to type, a `<textarea>` element to capture the user’s input, a “Start” button, and a `<p>` element to display the result.
Styling the Interface:
To make the typing speed test visually appealing, we’ll add some CSS. Create a new file called “styles.css” and add the following code:
[code]
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: #333;
}
textarea {
width: 400px;
height: 200px;
margin: 20px auto;
}
button {
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
#result {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
}
[/code]
In the above CSS code, we define the basic styling for the elements in our typing speed test program.
Handling User Input and Calculating Speed:
Now, let’s add the JavaScript code to handle user input and calculate the typing speed. Create a new file called “script.js” and add the following code:
[code]
// Define the quote that the user needs to type
const quote = “Lorem ipsum dolor sit amet, consectetur adipiscing elit.”;
// Get the necessary elements from the DOM
const quoteElement = document.getElementById(“quote”);
const inputElement = document.getElementById(“input”);
const startButton = document.getElementById(“startButton”);
const resultElement = document.getElementById(“result”);
// Function to start the typing speed test
function startTest() {
// Clear the previous result
resultElement.textContent = “”;
// Disable the start button
startButton.disabled = true;
// Set the focus to the input element
inputElement.focus();
// Store the start time
const startTime = new Date().getTime();
// Event listener to check the input against the quote
inputElement.addEventListener(“input”, function () {
const inputText = inputElement.value;
const quoteText = quoteElement.textContent.trim();
if (inputText === quoteText
) {
// Calculate the typing speed
const endTime = new Date().getTime();
const totalTime = (endTime – startTime) / 1000; // in seconds
const speed = Math.round((quote.length / totalTime) * 60); // in words per minute
// Calculate accuracy
const accuracy = Math.round((quote.length / inputText.length) * 100);
// Display the result
resultElement.textContent = `Speed: ${speed} WPM | Accuracy: ${accuracy}%`;
}
});
}
// Event listener for the start button
startButton.addEventListener(“click”, startTest);
[/code]
In the JavaScript code, we first define the quote that the user needs to type. Then, we get the necessary elements from the DOM using their respective IDs. The `startTest()` function is responsible for starting the typing speed test.
Inside the `startTest()` function, we clear any previous result, disable the start button, set focus on the input element, and store the start time. We also attach an event listener to the input element to check the user’s input against the quote.
When the input matches the quote, we calculate the typing speed by dividing the length of the quote by the time taken in seconds. We then calculate the accuracy by dividing the length of the quote by the length of the user’s input. Finally, we display the result in the designated `<p>` element.
Congratulations! You have successfully built a Typing Speed Test program using JavaScript. Users can now test their typing speed and accuracy by entering the provided quote. You can further enhance this program by adding more quotes or implementing additional features, such as a timer or a high-score leaderboard.
Remember to practice regularly to improve your typing skills. Happy coding!
In this article, we have covered the process of building a Typing Speed Test program using JavaScript. We started by setting up the HTML structure, added CSS for styling, and then implemented the JavaScript logic to handle user input, calculate typing speed, and display the result. The final program provides a simple yet effective way for users to measure their typing skills.