Deep JavaScript Foundations, v3

Function Expression 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 "Function Expression 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 write a set of comparable functions and function expressions.

Preview
Close

Transcript from the "Function Expression Exercise" Lesson

[00:00:00]
>> Kyle Simpson: So let's jump into our exercise on function expressions. Pull up the readme and the ex.js for the function expressions exercise. In this exercise, you're gonna be writing some functions and also some function expressions. And these functions are gonna be managing some student enrollment records for our workshop.

[00:00:23]
The spirit of the exercise is that you're gonna be using what we've learned about named functions and using functions wherever possible. So you wanna be using things like map and filter and find instead of your old school for loops. You wanna use functions wherever possible. This is broken up into two parts, this exercise.

[00:00:44]
First, complete part one and then complete part two. We'll come back, when we come back we will explain the solution for both so you will have time to work on both parts. In part one, what you're gonna do is write a function declarations and named function expressions according to the instructions here, and I'll go over those in a moment.

[00:01:04]
In part two, what you're going to do is rewrite all of those functions that you did as arrow functions. The purpose of part one and part two is for you to have a side by side comparison of the differences in code readability and functionality between using named functions and using arrow functions, okay?

[00:01:23]
So there won't be any difference in behavior, just simply a difference in code style between those two parts.
>> Kyle Simpson: The main functionality that you need to implement is that there are three functions, called printRecords, paidStudentsToEnroll, and remain, I'm sorry, remindUnpaid. Those three functions have been stubbed out in the ex.js file.

[00:01:46]
They're there for you, you see what they receive as arguments, I mean, as parameters. But you need to implement those according to what the readme says. And you'll notice that those functions are already called for you, so here I've called printRecords. I call paidStudentsToEnroll and I call remindUnpaid and the output that is expected is here in this code comment.

[00:02:09]
So your output needs to match this output, okay? What printRecords does is it takes a list of student IDs. So it's gonna take an array of student IDs as you see here, we are passing in an array of student IDs called currentEnrollment. So printRecords takes a list of student IDs and needs to retrieve each student record.

[00:02:30]
Those are stored in the studentRecords array. Retrieve each record by ID, and there's a hint there than you can use array find. Sorted by the student name, ascending. You can use array sort, but you're gonna have to use a custom function. And then print each record to the console or the console log statement including their name, their ID, and whether or not they're paid.

[00:02:56]
That's what that function does. Number two, the paid students to enroll function looks through all the student records and checks to see if any of them are paid but not yet enrolled. Meaning they're not yet in the currentEnrollment array, their ID isn't there. Collect those IDs and then return a new array with the previously enrolled IDs plus the new ones.

[00:03:15]
So, it doesn't modify, it returns a new array. And finally, the remindUnpaid is you're gonna take a list of student IDs and filter that list to only ones whose records are in the unpaid status. And then pass that list back to our printRecords from item one to print out only the students that are unpaid.

[00:03:38]
Okay, so this is part one of the exercise and again, part two, you don't change any behavior, you just simply rewrite it with arrow functions. Generally speaking, part one should take you about 15 minutes and part two should take you about five minutes. So overall we would take about 20 minutes to do this exercise.

[00:03:56]
So we'll break now, let you have a chance to work on exercise part one and part two. I'd recommend putting your solutions in two separate files so that you can compare and contrast them. We'll come back in a little bit and talk through the solution to this exercise.

[00:04:11]
Reminder that if you get stuck and you need a little bit of a hand through you just like to cheat, the solutions files are there in EXfix-1 and EXfix-2.

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