Check out a free preview of the full Functional-Light JavaScript, v2 course:
The "Challenge 4: Solution" Lesson is part of the full, Functional-Light JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle walks through the solution to Challenge 4.

Get Unlimited Access Now

Transcript from the "Challenge 4: Solution" Lesson

[00:00:00]
>> Kyle Simpson: So excercise four. You're given an empty implementation of pick numbers and asked to implement the logic as described. So you need to keep it pure other than of course the fact that there is randomness. Need to keep it pure and have it generate a new lottery number each times it's called adding it to the list.

[00:00:21] But it needs to keep that list in sorted order and also make sure there's no duplicates. So this was the setup given, lotteryNum will do that generation for you, generate numbers in the range of 1 to 58. And then pick number is the function you wanted to implement and lucky lottery numbers is the variables.

[00:00:46] It's the list that you want to add stuff into. [COUGH] So a couple of things we're gonna wanna pass in luckyLottertNumbers instead of have pickNumbers automatically assuming that because that would be a side effects. So for sure we're gonna wanna pass in luckyLotteryNumbers, but then what we talked about before was if we didn't see the implementation of pickNumber here, we might wonder whether or not somebody was going to mutate that list out from underneath us, which would be a bad thing.

[00:01:18] So we could increase the reasonability of this code, the trustability, by using object.freeze on our luckyLotteryNumbers. Now there is,
>> Kyle Simpson: That is then, since it's not gonna be creating the side effects in there, we're gonna need to save that result into another variable. Now at this point, I like to just overwrite luckyLotteryNumbers, with the return result.

[00:01:52] Because I'm explicitly saying it's okay for you to give me that list back, and I'm saying very specifically I know the list is updating. It's in a contained way it's updating, only in this for loop, that sort of thing. Functional programmers like to use consts, because they don't like to reassign to stuff.

[00:02:09] So a functional programmer actually would have probably modeled this as a list operation, like a map or something like that, where they wouldn't have needed to worry about it. But this is one of the reasons why I dislike the const keyword because I think it unnecessarily handcuffed me here.

[00:02:24] I would've had to have some other location to store all the intermediary versions of that luckyLotteryNumbers. Maybe in some temp variable or something. So, I'm just gonna go ahead and reassign my list each time and then I know at the end that my list is ready to print out.

[00:02:42] So, let's start defining pickNumber, we want to receive a list. Now first things first regardless of whether or not that was passed in as immutable or not, we wanna treat it as immutable. So we wanna make a new list of numbers. I'm just gonna call it nums and we're for sure gonna wanna return that list of numbers.

[00:03:08]
>> Kyle Simpson: Now we are only responsible for calling lotteryNum once because that's all that pickNumber does. It's just supposed to add one number to our list. But we do need to make sure that the number that we choose is not already in the list list, rather than add it to the list and have to unique the list and do it again or something.

[00:03:29] We wanna check to make sure that it's not already in the list. So that could be done with some simple looping logic. Basically generate a new lottery number, and if it's in the list, do it again, and do it again until we get a lottery number that's not already in the list.

[00:03:47] Your flavor of that might look a different. I'm gong to use a do while loop. So my while condition is gonna be; while the number that I'm about to generate is not in nums already. I'm sorry, while it is in nums, so nums.indexOf the number that I just generated is greater than or equal to 0 or not equal to -1.

[00:04:15]
>> Kyle Simpson: How are we you wanna check for? But we wanna make sure that we're gonna keep looping if we found that number. So, I'm gonna have that variable here and I'm going to just generate the new lottery number. So that loop's gonna loop over, and no matter how many times it has to run, it's gonna run until we generate a number that's not already in the list, and then exit.

[00:04:44]
>> Kyle Simpson: Now that I know that I have that number, I need to add to my list, since I am fully in control of the nums array, mutating that list is perfectly reasonable. If that array were not something I was fully in control of, I would instead want to concatenate onto that list and maybe save it into some other variable or something.

[00:05:09] That way I wasn't mutating somebody else's value. So either of those two ways works. This way is less performant. And the reason why I don't wanna do less performant is because I'm fully in control of that value so it doesn't matter to the outside world. It might matter to the readability of my picNumber function, but it won't matter to the outside world.

[00:05:30] So, there's a trade-off to be made, do I care about the performance? Let's assume that I do, that I wanna make sure that this runs as efficiently as possible. So, we can just mutate the num's RI.
>> Kyle Simpson: And then return the nums.
>> Kyle Simpson: So if I test that code over in a browser in the console.

[00:05:59] Did I type it wrong? Lottery known there we go.
>> Speaker 2: I don't think you're assigning the list to nums yet either.
>> Kyle Simpson: Maybe not. All right, cause I started that's a good point. I meant to come back to that. So I started out with an empty list, then I said, well, am I gonna copy over the list into nums?

[00:06:18] Or should I just start nums out as a copy of the list? That's a simpler way of doing it, so rather than start out with an empty list, I'm gonna copy over list and make that be the initial value for nums. So I'm gonna say list.slice, that's how I copy an array.

[00:06:34] There's multiple ways to copy, but slice will make me a new copy of an array. It's a shallow copy but a copy nonetheless.
>> Kyle Simpson: Now, the final thing that the read me said was make sure that those numbers are sorted in ascending order. So I need to make sure to sort the nums list.

[00:07:02] So I'm gonna say nums.sort
>> Kyle Simpson: And a quick and dirty way of sorting, is to do that subtraction. You could have done your if statements if you wanted to write your own sort. If you use the built in sort, understand that the built in sort is going to default to lexical graphic sorting, which means that the number one would sort after the number ten.

[00:07:36] It's not what you want, so to do a numeric sort you do need to actually do a numerical creation like that or do your if statements so you can just use the build in comparative.
>> Kyle Simpson: So let's try that again,
>> Kyle Simpson: Run it again. I'll just run it a few times and visually verify that I'm always getting a sorted list with no duplicates.