- The app we're building is a typing speed test, but typing speed is no good without accuracy so we're going to test for accuracy at the same time. What I'm going to do here is match the text that's being input into the test area with the text directly above. If the text is correct we'll have a blue line around the test area if a mistake is made the line turns orange and when the text matches exactly the text above so the test is complete, it'll turn green. The challenge here is as we're typing in content in this test area we need to match the shortened version of the content to a shortened version of the string above.
If we try to test it to the entire string it'll always be incorrect until the entire string has been typed up. We've already started this process in the spell check function so here we have the let textEntered which is the textArea.value. That is what is currently sitting inside the testArea. The next thing we need to do is grab the originText which sits in a constant called originText and place it inside a new variable and then truncate it. So I'll set up a new variable called originTextMatch and I'll set that equal to originText and here I'll use the substring method.
Substring treats a string of text as an array and allows us to specify a section within the text to pull out and use as a substring. It takes two arguments, where in the array we want to start, which in this case is zero to start, and how many characters we want returned. We already know how many characters because we have textEntered up here and we just want the same amount of characters as textEntered, so I'll just say textEntered dot length.
This gives us the length property and as a result we should now have two equal length strings. That means we can now test to see if the string inside the test area matches the string outside the test area. Here we'll first test to see if the string inside the test area matches the originText in total because in that case we're done with the test. So if textEntered double equals originText in that case we take testWrapper, that's the wrapper that's going around the test area that is currently grey, we'll set its style property and the border color to a green color.
If this condition is not true we need to test whether textEntered matches originTextMatch. So else then if textEntered double equals originTextMatch, this is the shorter string, and again, we'll grab testWrapper border color and if this is true we set it to a blue color. 65CCF3 And if it's false we set it to an orange color.
Finally, I'll just take away this console log 'cause we don't need it anymore, save and now we can test it in the browser. So if I type in a string that matches we should see a blue outline, but if I make a mistake it should turn orange and I can go back and fix it, and if I match the strings exactly it turns green. At this point the test should stop, we haven't gotten that far yet, but at least now the color feedback works properly, and as you can see it's very easy for a tester to know exactly what's going on here.
Good, that means we can move on.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Detect spelling errors by matching strings