Can someon please explain to me why the the politelyDecline function is called within the declineEverything function without a parameter. Should it not return the string four times with the veggies being undefined since no argument was passed?
Thank you.
edit: since I could not solve this one I let codecademy give me the solution. The solution however wrinkles my brain because of the above.
Hello, right now i am attempting stage 6/14 of the intermediate JavaScript challenges, the lesson explains how you must print a message from politelyDecline() to iterate through the veggies array to print an array for each vegetable. For example: “No broccoli please. I will have pizza with extra cheese.” but with all of the vegetables iterated. I have managed to get that expected output, however, it tells me that i am still incorrect even though my output is as directed. I’m not sure if i am doing anything wrong, but i have checked spelling, I have tested with different ways of writing the callback function but with no success. I have not viewed the solution because this would mean resetting the exercise to try again, but the task is still tagged as completed in the check box meaning i do not know if my code is correct if i try to run it again. Any help would be greatly appreciated, thank you!
My code:
const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];
const politelyDecline = (veg) => {
console.log('No ' + veg + ' please. I will have pizza with extra cheese.');
}
// Write your code here:
const declineEverything = (array) => {
array.forEach(veggie => {
politelyDecline(veggie)
})
}
declineEverything(veggies)
// outputs:
/*No broccoli please. I will have pizza with extra cheese.
No spinach please. I will have pizza with extra cheese.
No cauliflower please. I will have pizza with extra cheese.
No broccoflower please. I will have pizza with extra cheese.*/
lesson error message replies with “Does your function pass the politelyDecline() function into forEach() ?”
even though it is passed through.
In reply to myself and hopefully helpful to others. The following is inherent to the .forEach method that answered my own question: .forEach() loops through the array and executes the callback function for each element. During each execution, the current element is passed as an argument to the callback function.
So in short: the forEach method passes the element as an argument to the callback function.
I was wondering if anyone can help me figure out why my code keeps acting the way it is. It’s printing out what it should, but also “undefined” at the end. It’s only happening with declineEverything.
Code
const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];
const politelyDecline = (veg) => {
console.log('No ' + veg + ' please. I will have pizza with extra cheese.');
}
// Write your code here:
const declineEverything = (arr) => {
arr.forEach(politelyDecline)
}
console.log(declineEverything(veggies));
const acceptEverything = (arr) => {
for (const i of arr) {
console.log(`Ok, I guess I will eat some ${i}.`);
}
}
acceptEverything(veggies);
Output
No broccoli please. I will have pizza with extra cheese.
No spinach please. I will have pizza with extra cheese.
No cauliflower please. I will have pizza with extra cheese.
No broccoflower please. I will have pizza with extra cheese.
undefined
Ok, I guess I will eat some broccoli.
Ok, I guess I will eat some spinach.
Ok, I guess I will eat some cauliflower.
Ok, I guess I will eat some broccoflower.
Oh wow – I feel silly! I seemed to forget that politelyDecline was logging the sentences to the console, so of course I didn’t need to call it again, since declineEverything isn’t returning anything. Thanks for your response.
veg is a parameter for a function. It is the local name given to the argument…
politelyDecline(veggies)
veggies is the argument of the function call expression. We are passing a reference to the parameter so that the array can be accessed from inside the function. We are not passing in the array, though, just a pointer to its location in JS’s namespace.
I have the same question. The first piece of code outputs exactly the same as the given solution, also with the ‘undefined’ at the end.
I’m also wondering if there is something to do to not have undefined printed out at the end of the output? I know forEach() returns undefined, but does that mean we cannot do anything to not have it printed out?
has the callback function written directly in the argument
has the callback function written as a standalone, with only the reference given in the argument. It will be invoked on each iteration. The standalone would look like,