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.

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)

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.

2 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.

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:

1 Like

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?