Unable to load the content

Please attempt to reload the page or alternatively, try accessing it using a different browser.

How to Build a Typing Speed Test App using HTML CSS JS

Creating a typing speed test application can be an interesting and educational project for developers, helping you practice your coding skills while implementing real-time functionality. 

In this post, I will take you through the process of developing a web-based typing speed test app. This app helps users test their typing skills by tracking their words per minute (WPM), characters per minute (CPM) and accuracy during a timed typing challenge. Let's break down the key concepts and logic involved in creating this app.

Read More: Unlock Web Animation Secrets: Top 10 best CSS Animations with Source Code

How-to-build-a-typing-speed-test-app-using--html-css-js

Setting Up the HTML Structure

To begin, we need to create the essential HTML structure that will house our app. The app will consist of two main sections:

<div class="container">
    <h1>Test Your Typing Skills</h1>
    <div class="inner-wrapper"></div>
</div>
<div class="modal-container">
    <div class="modal">
      <h2>Your Result</h2>
      <span class="bi bi-check2-circle"></span>
      <div id="result">
        <div class="result-card" id="wpm">

  • Test Area: Where users can type the sentences and see the progress.
  • Results Area: Where users will see their typing speed and accuracy after completing the test.

We have a container for the main content that will show the test instructions and the typing text. Additionally, there’s a modal that will display the results at the end of the test.

Handling Sentence Selection and Display

The main objective of the typing test is to type random sentences accurately and quickly. To achieve this, I created an array of sentences and randomly select a sentence for the user to type. Once a sentence is selected, it is displayed on the screen character by character.

Each character is wrapped in a span element with a class of .char. This allows us to easily track individual characters during the typing process and apply different styles based on the correctness of each character typed.

Additionally, the next sentence is preloaded so that when the user completes one sentence, a new one appears immediately. This ensures the test continues seamlessly without delays.

How-to-build-a-typing-speed-test-app-using--html-css-js

CSS Styling

To create an engaging and visually appealing typing speed test application, CSS plays a crucial role. Here's a breakdown of how different elements can be styled to enhance user experience and interface aesthetics.

.container {
  padding: 20px;
  width: 100%;
  max-width: 600px;
  border-radius: 8px;
  background-color: #fff;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.inner-wrapper {
  display: grid;
  margin-top: 15px;
}

Capturing User Input and Tracking Progress

Once the timer starts, the app listens for the user's keystrokes using event listeners. The key event handler ensures that only valid characters are typed and it prevents non-printable keys (such as Shift, Ctrl or Alt) from affecting the typing test.

For each keypress:

  • We check if the typed character matches the one on the screen.
  • If the character matches, it's marked as "correct," and we increase the correct count.
  • If the character doesn't match, it's marked as "incorrect," and we increase the incorrect count.
  • The cursor also moves automatically to the next character as the user types, creating a smooth experience.

Handling Backspace

The backspace functionality was implemented to allow users to correct their mistakes. When the backspace key is pressed, the app checks if the cursor is positioned at the start of a word or a character. If the backspace is valid, it removes the correct or incorrect class from the last typed character.

By removing the correct or incorrect classes, we also adjust the counts for correct and incorrect characters accordingly.

JavaScript Implementation

To bring the Typing Speed Test Web App to life, JavaScript plays a critical role in managing the logic, user interactions and dynamic updates. From starting the timer to tracking user input, every functionality is powered by well-structured JavaScript.

const elements = {
  innerWrapper: document.querySelector(".inner-wrapper"),
  resultDiv: document.getElementById('result'),
  wpmDiv: document.getElementById('wpm-value'),
  cpmDiv: document.getElementById('cpm-value'),
  accuracyDiv: document.getElementById('accuracy-value'),
  popupCloseBtn: document.querySelector(".close-btn"),
  tryAgainBtn: document.querySelector(".try-again-btn")
};
const durationOptions = [30, 60, 120, 240];
let state = null;

The result screen displays the following metrics:

  • Words Per Minute (WPM): Calculated as the total number of correct words typed divided by the duration in minutes.
  • Characters Per Minute (CPM): Calculated by counting the total number of characters typed (correct and incorrect) and dividing by the test duration.
  • Accuracy: This percentage is calculated by dividing the correct characters by the total characters (correct + incorrect) and multiplying by 100.
  • Displaying Results and Giving Options to Try Again or Exit

At the end of the test, the app shows a modal with the results. It displays the WPM, CPM and accuracy to the user. There's also a "Try Again" button, which allows users to restart the test and improve their scores. Additionally, there’s a "Close" button that exits the test and resets the app.

Resetting the Test

To allow users to restart the test, I implemented a reset function that clears all the previously entered data, including the timer and user inputs. The app then reloads a new test, resetting the sentence selection and starting the timer from scratch. This ensures that the test can be taken multiple times without any issues.

The Complete Experience

The goal was to create an interactive, fun and engaging typing speed test. The app:

  • Allows users to select a test duration.
  • Provides real-time feedback as they type, highlighting correct and incorrect characters.
  • Displays an interactive countdown timer and updates it as the test progresses.
  • Calculates and displays typing speed (WPM, CPM) and accuracy.
  • Lets users try again or exit the test with ease.

Conclusion

Building this typing speed test web app required combining several web development concepts, including DOM manipulation, event handling, timers and CSS animations. It was a fun project that not only helped me improve my JavaScript skills but also created a useful tool for anyone looking to challenge and improve their typing speed.

Read More: Create an Expandable Sidebar Menu using HTML CSS and JS

I hope this breakdown gave you an insight into the logic behind developing this web app. If you have any questions or need further clarification, feel free to ask in the comments.

Download