Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialconah rivers
6,747 PointsCallbacks makeMeal quiz question
I hate asking for answers to quiz questions but this one has been bugging me for quite some time, and I cannot work out the answer from reading over the callback review instructions.
The quiz at the end of this section asks you to "Set the makeMeal function to invoke the callback function passed to it":
function makeMeal(meal, cb) {
alert(`Preparing ${meal}.`);
\\insert cb code here ;
}
makeMeal('lunch', () => {
alert('Finished the meal!');
});
looking over the callback review instructions it would seem that the 'preparing meal' alert needs to pop up first, and then the 'finished meal' alert pops up last, but when I try to invoke the cb function noted above by typing things such as
cb(makeMeal());
or
cb(lunch);
it doesn't work?
The question seems simple but I am having trouble understanding the basic implimentation, I would appreciate it if someone could give me some hints!
Thank you in advance.
7 Answers
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,880 PointsIt is a part of makeMeal
in the makeMeal function there is (meal, cb). So if you need to invoke the function for cb, you need to enter:
return cb();
conah rivers
6,747 PointsIt turns out I overthought this question way too much as it is much simpler than I initially thought. I won't give it away but the key thing to concentrate on after reading :
set the makeMeal function to invoke the callback function passed to it
is the word 'invoke'.
I'll leave this question up incase someone else gets stuck:)
ddeveloper
Full Stack JavaScript Techdegree Graduate 15,281 Pointsi am stuck on this. What is the resolution?
Krisitans Engels
10,670 Pointsreturn cb();
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,880 PointsI know it is not recommended to give the answer but sometimes I've been stuck myself and seeing the answer clear it all up.
Michelle Mashamba
10,148 PointsI'm stuck someone to help
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,880 PointsMichelle Mashamba , as I've come to learn, the vocabulary used by the instructors is important to pick up on. So for this test you have to:
"Set the makeMeal function to invoke the callback function passed to it"
so you have function makeMeal with two parameters passed into it (meal, cb)
the first line in the function is an alert which takes in the parameter meal, which when invoking the function, would be what is passed in for meal for example makeMeal(lunch, cb)
so to invoke makeMeal you would need to end the function using the parameter cb
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,880 Pointsddeveloper , it took me a bit but if you remember the code of the previous videos, you will see an example of this.
So it is asking you to "invoke the callback function."
That being said:
function makeMeal(meal, cb) {
alert(Preparing ${meal}.
);
\ here you need to put the code which would lead to the function below
}
makeMeal('lunch', () => { alert('Finished the meal!'); });
If makeMeal(meal) is makeMeal(lunch) that means makeMeal(cb) is makeMeal(function)
So what needs to be the answer?
ddeveloper
Full Stack JavaScript Techdegree Graduate 15,281 Pointsit should be makeMeal
Hugo Magalhaes
6,064 PointsAlso helped me. Thanks you guys
SABINA MUZIKA
9,795 PointsSABINA MUZIKA
9,795 PointsThank you Edgardo