JavaScript: From Fundamentals to Functional JS

# Callbacks Exercise Solution

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Callbacks Exercise Solution" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

## Bianca walks through the solution to the Callbacks Exercise. She also briefly discusses one of the solutions from the Closure Exercise completed earlier.

Get Unlimited Access Now

Transcript from the "Callbacks Exercise Solution" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Bianca Gandolfo: So we were just doing the last closure exercise.
>> Bianca Gandolfo: Which is to use the module pattern to create a toaster. And there's a bunch of different ways you can do this, this is just sort of like expressing, I don't know, your creative side when making your program.

[00:00:34] So this is an example of a toaster that has a maxTemp of 500. So, how we'd call this, we'd say, var, this is actually gonna be toaster. So var myToaster = toaster : and then you can say myToaster.setTemp I'm sorry. This should also be private. SetTemp = I don't know, 300 and then so, I'm sorry, we should pass the 300 like this, since it's a function.

[00:01:32] So myToaster.setTemp, we're gonna pass it 300. So and this says if the newTemp here.
>> Speaker 2: We wanna come after calculate.
>> Bianca Gandolfo: Nope because good point, there we go. Thank you. So we have set temp we're gonna pass at 300. So, newTemp is now 300 and it says, if newTemp is greater than maxTemp, maxTemp is 500 or console.log, that temp is too high, otherwise temp = newTemp.

[00:02:15] And so, what this does is this prevents people from being able to set the temperature of your toaster to too high. Anyone else? How else do people implement this? A different implementation? Rich I think you had one, what was yours?
>> Rich: I just kind of modeled it over the car one.

[00:02:36]
>> Bianca Gandolfo: Mm-hm.
>> Rich: And let the user increment the like the light dark type things?
>> Bianca Gandolfo: Okay, so like the timer kind of?
>> Rich: Yeah, exactly. And then they're gonna input like if it was just one slice then it wasn't gonna go for as long, and if it was two slices it would or whatever.

[00:02:59] But it wouldn't let the user mess with the inner workings of how long the toaster should run. Your user can't change the next temp, so overrun it.
>> Bianca Gandolfo: Right, cool, awesome, so that is really an example of making a class like constructor and JavaScript which is cool. The second thing we were gonna do that we did was the callback exercises.

[00:03:37] So I'm just gonna paste this in. Do this really quick.
>> Bianca Gandolfo: All right. So the first one says, write a function, funcCaller, that takes func which would be a function, and an arg which could be any kind of data, and the function returns func called with the arg as an argument.

[00:04:07] So the first thing we need to do is to write a function called funcCaller. So that just equals the function and again it takes the func, and then it takes an arg.
>> Bianca Gandolfo: And excuse me, and then we are going to call func with arg and return it.

[00:04:42]
>> Bianca Gandolfo: So that's the first one. The second one is the function called firstVal. So we say firstVal equal the function, right? There's our function body and our first parameter is going to be an array and then the next one is gonna be a function, and we're gonna call the function with the first value of the array, right?

[00:05:08] So, array, at the zeroth index. And we're gonna call that index which is gonna be zero. And then we're gonna pass the entire array.
>> Bianca Gandolfo: Cool?
>> Rich: And that's where I kind of got tripped up.
>> Bianca Gandolfo: Do you have a question about it?
>> Rich: Yeah, I guess it's maybe the wording of the thing so it's gonna call a function with just those three arguments, one half will be the first one.

[00:05:40]
>> Bianca Gandolfo: Yeah.
>> Rich: Next one, I don't understand index pound index number.
>> Bianca Gandolfo: Yeah, index number. And then the whole array, yeah. Cool. And, so now we're going to write a different version of first file, that doesn't matter if it's an array or object. So, we're just going to call it a list.

[00:06:05] It's gonna take a func.
>> Bianca Gandolfo: So what we can do here, we can say if we can say like Array.isArray. So this is just a method that checks if something is an array. So, we can just pass only pass list to it and then it will return true if it's an array.

[00:06:28] So if it's an array, we're just gonna do like the same as before right. We're just gonna pass the first one. Else, we're going to deal with it if it's an object. So we're just assuming that it's either array or an object. So one way we could do this, we could just loop through, so var k in list.

[00:07:01] So we can just loop through the list, and then we can just return out of it, return. And this would just loop once. K, our list[k], so this is just a property value, and this is the property, and this is the entire list. And if you return out of it, then it will stop looping, then we can just return this one too, so it's consistent.

[00:07:29] So that's one way to do it, you can also just do, so there's this one method called object.keys, and then you can pass the list there, and this returns an array of all of the properties, so we can call this propArray and then we can say, then we can say, func and then we don't need to return it.

[00:07:59] We say func proper a, func list, and this is getting the value. List prop array at I, prop array at I which, again, is the proper name. So, object.key. So what it does is it returns an array of all the keys in that object. And then list. So, that's just another way that you can do it.

[00:08:37]
>> Rich: Where are the I's coming from.
>> Bianca Gandolfo: I'm sorry, it should be zero. I'm just so used to doing I, thank you. Yeah, zero.