FAQ: Code Challenges: Intermediate JavaScript - declineEverything() and acceptEverything()

This community-built FAQ covers the “declineEverything() and acceptEverything()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise declineEverything() and acceptEverything()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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.

7 Likes

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.

It’s not immediately obvious, but what that message refers to is the callback that is passed in to the forEach() method.

array.forEach(callback)
1 Like

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.

9 Likes

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.
1 Like

This function has no explicit return value so returns None.

Oh wow – I feel silly! :man_facepalming:t2: 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. :slightly_smiling_face:

2 Likes

Ok, I got stuck at the same problem, so: thank you this is helpful!

So, if my code is:

const declineEverything = arr => {
  arr.forEach(v => politelyDecline(v));
};

and the output is the same as arr.forEach(politelyDecline)

Why is not correct too?

1 Like

I’m a bit confused with the veg code for this one. How does veg get the veggie array when veg and veggie are completely different.

const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];

const politelyDecline = (veg) => {
      console.log('No ' + veg + ' please. I will have pizza with extra cheese.');
}

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.

3 Likes
const declineEverything = (array) => {
  array.forEach(veggie => {
    politelyDecline(veggie)
  })
}

cant i use this instead. i understood how

array.forEach(
    politelyDecline)

works. :sweat_smile:

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?

1 Like

I have another way which makes the code works but can you explain why this solution gives the same result but didn’t work please?

function declineEverything(veg, pol){

veg.forEach(veg => {

pol(veg)

}

)

}

console.log(declineEverything(veggies, politelyDecline))

1 Like

Did you return something … it supposed to work

i also got this question

yeah… but I had to use another solution and gone past this stage now…

can you please help me to understand what is diference between these two syntex?

array.forEach(politelyDecline)               //works, no warning
vs 
array.forEach(a => politelyDecline(a))       //works, with warning

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,

const politelyDecline = a => {
    console.log(...)
}
1 Like