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


#1

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!


#2

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.


#3

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.


#4

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)

#5

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.