Typing Speed Detector App Using Typescript (original) (raw)
Last Updated : 05 Feb, 2025
Typing speed has been a very important test for Engineers as a faster typing skill leads to faster code so, To test this speed we are building a typing speed detector.
What We’re Going to Create
- An Input Box for Typing Our Text.
- A Time Element to show the time.
- An Element to show Words per minute.
- A start Button to start the test.
Project Preview
Typing speed detector using Typescript
Typing Speed Detector - HTML and CSS code
This is a Typing Speed Test web app where users can measure their typing speed in Words Per Minute (WPM). It includes a timer, input area, and a start button to begin the test.
HTML `
Typing Speed Test
Press Start to begin typing
Time: 0s
WPM: 0
Start`
**In this Example
- A centered .box container with a clean design, including a heading, sentence display, text input, and stats section.
- The #inp textarea is disabled initially and gets activated when the user clicks the Start button (#start).
- The #tm (Time) and #wpm (Words Per Minute) update in real time as the user types, tracking their typing speed.
Typing Speed Detector - Typescript logic
This code creates a dynamic grid that allows users to draw on it by changing the grid size and color. It supports interaction with mouse events for drawing and resetting the grid.
Typing.ts `
const c = document.querySelector(".container"); const s = document.querySelector('.size'); const col = document.querySelector('.color'); const rBtn = document.querySelector('.button');
if (!c || !s || !col || !rBtn) { throw new Error("Missing required DOM elements"); }
let sz: number = parseInt(s.value); let d: boolean = false;
function g(): void { c.style.setProperty("--size", sz.toString()); c.innerHTML = "";
for (let i = 0; i < sz * sz; i++) {
const b = document.createElement("div");
b.classList.add("box");
b.addEventListener('mouseenter', () => o(b));
b.addEventListener('mousedown', () => m(b));
c.appendChild(b);
}
}
function o(b: HTMLDivElement): void { if (!d) return; b.style.backgroundColor = col.value; }
function m(b: HTMLDivElement): void { b.style.backgroundColor = col.value; }
window.addEventListener('mousedown', () => { d = true; });
window.addEventListener('mouseup', () => { d = false; });
function r(): void { g(); }
rBtn.addEventListener('click', r);
s.addEventListener('change', () => { sz = parseInt(s.value); r(); });
g();
`
**In this example
- The g() function generates a grid of sz × sz cells, clearing the previous grid before adding new ones.
- The grid size updates dynamically based on user input (.size input field).
- Cells change color (col.value) when hovered over (mouseenter) or clicked (mousedown).
- A boolean d tracks whether the mouse is pressed, allowing smooth drawing.
- Clicking the Reset button (.button) clears and recreates the grid.
- It ensures users can start over without reloading the page.
- When the user changes the .size input, the s.addEventListener('change', ...) triggers a new grid update.
Convert to JavaScript File
Now You need to convert the TypeScript file into JavaScript to render by browser. Use one of the following command.
npx tsc typing.ts
tsc typing.ts
- The command typing.ts compiles the typing.ts TypeScript file into a typing.js JavaScript file.
- It places the output in the same directory as the input file by default.
Complete Code
HTML ``
Typing Speed Test
Press Start to begin typing
Time: 0s
WPM: 0
Start