Deep JavaScript Foundations, v3

Equality 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 "Equality 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:

Kyle introduces the exercise on equality, which is to handle corner cases in equality comparison.


Transcript from the "Equality Exercise" Lesson

>> Kyle Simpson: Let's dig in a bit on this topic of equality, because there's a lot of meat that we just went over in terms of equality. So we wanna have a whole exercise dedicated to this. And fair warning, this exercise is going to be fairly involved. There's a lot of little nuances that we're gonna have to take care of.

What I am asking you to do with this exercise is you're going to write a findAll utility. And this findAll utility searches through an array, looking for any values within the array that are coercively equal to some test value that you've passed in. But there are some very specific constraints on that coercive equality.

So it's, of course, not as simple as just applying a double equals. So the findAll utility needs to return an array with all of the values that match the test rules as described here in the instructions. So the coercive matching that we want, which is not just the double equals, is any exact matches, which we now know we can use an object dot this for.

And any exact matches, we, of course, want to include. If there are any strings, except for the empty string or white space only, remember, we wanna ignore those, but if there are any strings, they're allowed to coercively match numbers. And if there are any numbers, other than NaN and the infinities, those are allowed to match strings.

But as a little bit of a hint, be very careful with -0, because it will throw a wrench into several of these test cases, okay? We wanna allow null to match undefined, and vise versa, that's pretty straight forward. We wanna allow booleans only to match other booleans. So true only matches true, not the string true, or the number one, or anything like that.

And anything that's not a primitive, like any of the objects, those are strictly identity only matching. So no structural checking that you need to implement. In our ex.js file, you'll notice that we have a bunch of test cases here. And there's a function at the bottom which is only being used for the purposes of the test case.

It's checking to make sure that these arrays contain the same values, and then even if they're in different orders. So these are all of our test cases that we're checking for. It would be useful for you to go through those test cases to make sure you understand what the readme is saying.

But you wanna do is implement a findAll which can take this values array and a value like, for example, let's just take one here. So the findAll takes the -0 and the values, and it should only return an array with a single -0 in it. But for example, if you pass findAll with "0", then that should find both the regular 0 and the quote 0, okay?

Hopefully that makes sense. So we will take a bit more time because this is more involved. You're gonna write several different clauses and if statements to handle all of these. And don't feel bad if you miss some of these cases, cuz there's a lot of nuance here. The point of the exercise is to get you thinking about, how do I restrict this so that I have a safe set of coercive comparisons?

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