Lesson 5 of 10
Quote Typer
Type a quote and get feedback on every keystroke. Green means correct, blue means keep going, red means check your spelling.
What you will learn
- Arrays of objects
- Math.random()
- The input event
- startsWith
- Changing text and colour
Before you start
Zoo Webpage. You handled a click there. Here you handle typing, which fires an event on every key.
What this page does
- The page shows a random quote, its author, and its book.
- Type the quote into the box. The message under the box changes as you type.
- Green means you typed the whole quote correctly. Blue means what you have typed so far is right. Red means a letter does not match.
- Click the refresh button for a new quote. The box clears.
How it works
- The quotes are in an array. Each item is an object with
text,author, andbook. - When the page loads, the code picks a random quote. It stores the quote text in
active_quote, so the checker always knows the correct answer. checkTyping()runs on every keystroke. Theinputevent fires each time the box changes.- The checker compares your text with
active_quoteand picks one of three messages.
if (typed === active_quote) {
// Case 1: the text matches exactly. You are done.
check_message.style.color = "lightgreen";
} else if (active_quote.startsWith(typed)) {
// Case 2: what you typed is the start of the quote. Keep going.
check_message.style.color = "lightskyblue";
} else {
// Case 3: something does not match.
check_message.style.color = "salmon";
}Make it easier to read
This is one line of CSS: `font-size` on `#quote_text`.
1rem is the browser's normal size, so 2 is twice as big.
Build it step by step
Make the page
Make
index.htmlwith three paragraphs for the quote, the author, and the book. Add the refresh button, the typing box, and one more paragraph for the message. Put the<script>tag at the end.Make the quote list
In
quote.js, make thequotesarray. Each object needstext,author, andbook. Use any quotes you like. If a quote has no book, use an empty string"".Show one quote
Get the three paragraphs. Pick a random position. Store the quote text in
active_quote. Then write the text, the author, and the book into the page. Reload. A random quote appears.Wire up the refresh button
Move the code from step 3 into a function called
refreshquotes(). Connect it to the button withaddEventListener("click", refreshquotes). Clicking now changes the quote.Add the typing checker
Write
checkTyping()with the three cases. Connect it withtype_box.addEventListener("input", checkTyping). The message now updates while you type.Clear the box on refresh
Add two lines to
refreshquotes()so it also empties the typing box and the message. Each new quote now starts clean.
Things you can tidy up
This file has a few leftovers from when it was written. Cleaning them up is good practice.
- The
console.log(quotes[0])line and the commented-outconsole.loglines were used for testing. You can delete them. type_boxandcheck_messageare used insiderefreshquotes()but declared further down the file. This works, but it is clearer to declare them before the function that uses them.- Two comments have spelling mistakes: "betweeen" and "pervious".
- There is a comma after the last quote object. It does no harm, but it is not needed.
Try these changes
Make these in the files beside this guide. None of them need anything the lesson has not covered.
- Add your own quotes to the array.
- Show how many characters you still have to type.
- Time the attempt and show words per minute.
- Hard: colour each letter green or red as you type it.
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Show the
inputevent before writing the checker. Attach a listener that only doesconsole.log(type_box.value)and let the student watch the console fill up as they type. - Teach
startsWithin the console first. Run"hello world".startsWith("hell")and then"hello world".startsWith("help"). The two answers are the whole lesson. - Add the three cases in order: exact match, then prefix match, then the else. Test after each one. If the else comes first, the other two never run and the student cannot see why.
- The cleanup section is a real exercise, not filler. Ask the student to find the leftovers before you show them the list.