Deep JavaScript Foundations, v3

Coercion Exercise

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Coercion Exercise" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to define two validation functions.

Preview
Close

Transcript from the "Coercion Exercise" Lesson

[00:00:00]
>> Kyle Simpson: Let's take a look at coercion a bit more deeply with this exercise. We've learned now a bit about how coercion works with different operators, like the less than operator and things like that. So we wanna do some validation functions to make sure that some inputs that are coming, for example on a Web page that are coming from DOM inputs, we want to make sure that some inputs that the user is giving to us are valid.

[00:00:29]
So we're going to define two different validator functions. The first one's called isValidName. And it gives the requirements here, needs to take a string that is non-empty, must contain something that's not white space, and has to be at least three characters long, okay? So it has to return true if what you pass in matches that, and otherwise return false.

[00:00:54]
And again, the spirit of this exercise is, use what we've learned now about coercion to properly handles this. So there's a variety of ways to do it, but try to properly handle that check. Then you're gonna do a slightly more complex validator called hoursAttended. It takes two inputs, we'll call them attended and length.

[00:01:14]
It allows either one of these to be a string or a number. So we are gonna allow a bit of coercion here. We're not gonna say it has to always be a number. We'll allow both strings and numbers, but nothing else. So we need to check to make sure, or assume that those are definitely strings and numbers.

[00:01:32]
Whether they come in as numbers or not, we're going to treat them as numbers. So this is not a case we throw an assertion if you pass it wrong. We're just gonna say the contract here is, you've gotta pass this as a string or a number, and then treat it as a number.

[00:01:45]
Those numbers have to be 0 or higher, so no negative numbers. And also, they have to be whole numbers. We don't wanna pass in something like 3.14, okay? And then, finally, attended has to be less than or equal to length. So those are your two validators that you're gonna define, trying to practice what we've learned so far about coercion.

[00:02:09]
We'll take about ten minutes on this. As you can see in the exercise file, we have console.log statements which are our test cases, essentially. This is the poor man's approach to testing, if you will. So all these console log statements should print true if your functions are correct.

[00:02:27]
Define those two functions. Drop this code into your favorite console or run it in Node, and you should get it printing all trues. We'll take about ten minutes for this exercise, and then we'll come back and talk about the solution.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now