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

Thank you for your quick answer.

1 Like

my question is also same how the veg get veggies as it is use in decline every function and didnt pass argument there

Hey,

I am having trouble understanding why this wouldn’t work:

const acceptEverything = arr => {
arr.forEach(function(veggie) {
console.log(Ok, I guess I will eat some ${veggie})
})
};

console.log(acceptEverything(veggies));

The console shows:

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
undefined

First of all, why is there an undefined? Second, why exercise shows error?

In this lesson, we’re asked to write a function declineEverything() that takes in an array of strings and, using .forEach() , loops through each element in the array and calls politelyDecline() with each of them:

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/decline-accept-everything

In order to accomplish this task, we have to write the following code snippet:

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)
};

declineEverything(veggies)

and it prints the following:

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.

Could anyone explain why don’t we use parentheses () for politelyDecline while passing it in into our arr.forEach() method?

If I put the parentheses () there, it returns undefined and prints the following:

No undefined please. I will have pizza with extra cheese.

Why so?

politelyDecline has one parameter. Shouldn’t we put parenthesis () while using politelyDecline as a callback function?

.forEach() is an iterator method that runs the callback function once for each item in the context array.

context.forEach(callback)

We do not invoke the callback, the iterator does, hence we only pass the function name itself (pass by reference). During iteration the callback is called with the current value as the argument.

1 Like

yeah this was so hard for me to understand… I didn’t understand the (directly) part. Next time I encounter this, I now know what they mean by directly. Thanks google!.. I was frustrated because codeacademy has a link to review forEach but there is no example of this problem there to review. I learn by examples. I read things but if I don’t see it visually i can’t picture the structure. So I’m always looking for examples of how to structure code.

2 Likes

Good morning,

can someone please explain why the . (full stop) is an unexpected token in the code below as it is proper syntax?
Code line with error:
veggies.forEach(veggie) => politelyDecline(veggie);

Full code:

Full error:
veggies.forEach(veggie) => politelyDecline(veggie);
^
SyntaxError: Unexpected token .
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

This is not the first time proper syntax results in an unexpected token error and I just don’t understand why. Any help would be greatly appreciated.
Mel

Hi, I was able to solve this and the exercise let me move forward however my console is still logging 2 undefines. This is my code:

const declineEverything = arr => {

arr.forEach(politelyDecline)

};

const acceptEverything = arr => {

for (i = 0; i < arr.length; i++) {

console.log(`Ok, I guess I will eat some ${arr[i]}.`)

}

};

And this is what it logs:

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

I have no idea where the undefines are coming from especially considering when I check the solution my code is nearly exact to theirs.

I was stuck on the same thing but realised I was using “console.log(declineEverything(veggies))” rather than just calling the function “declineEverything(veggies)”. Maybe that’s what you’re doing? From what I understand written elsewhere on this thread, when you console.log the function it will run through the function and then give you the return value as well, which is undefined by default when using .forEach. If you call the function (i.e. don’t console.log it), it will only run the function without logging the return value.

I hope that makes sense and is correct? (It’s my first reply to a post but I was stuck on it as well!)

1 Like

I am having a difficult time figuring out why my code below is behaving the way it is.

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

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

const grudginglyAccept = (veg) => {
  console.log('Ok, I guess I will eat some ' + veg + '.');
}

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

const acceptEverything = array => {
  array.forEach(grudginglyAccept);
}

declineEverything(veggies);
grudginglyAccept(veggies);

This outputs the following:

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.
Ok, I guess I will eat some broccoli,spinach,cauliflower,broccoflower.

Maybe I am missing something, but I believe I have implemented both the decline and accept solutions in the same way, yet for some reason the grudinglyAccept() function is only calling the callback function in forEach once with the entire array. What am I missing?

There is no .forEach() in grudginglyAccept(), only the array parameter, hence the singular output.

Ugh…Thank you. Feeling dumb - should have called acceptEverything in the last line instead of grudginglyAccept!

1 Like

Hi all,

I keep getting this message Does your function log to the console in the following format for each element: ‘Ok, I guess I will eat some [element].’? Make sure your spelling and punctuation match exactly. and can not move forward, but I can’t see why it’s wrong?

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

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

const declineEverything = decline => {decline.forEach(politelyDecline)
}

const acceptEverything = veg => {
      console.log('Ok, I guess I will eat some ' + veg + '.');
}

const happilyAccept = accept => {accept.forEach(acceptEverything)
}

declineEverything(veggies);
happilyAccept(veggies);

This is what the console logs:

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

Can anyone see what spelling or punctuation I am missing?

Change the function names happilyAccept to acceptEverything

1 Like

Thank you! I did have to look over it a couple of times but I see where I’ve mixed up :slight_smile:

1 Like

Can someone please explain to me why it is that my code logs exactly what it should but is not the correct answer?

I see that the solution applies .forEach on the initial functions parameter then uses another function for the console.log statement rather than doing it on the array itself like I did and just using one function but I’m not quite understanding why… or why mine is incorrect.
Thank you for any feedback!

MY CODE:

const acceptEverything = veggies.forEach(veg => {
  console.log(`Ok I guess I will eat some ${veg}`)
})

SOLUTION CODE:

const acceptEverything = arr => {
  arr.forEach(e => {
    console.log(`Ok, I guess I will eat some ${e}.`)
  })
}