Decline Everything and Accept Everything

Hi all,
I’m working on Practice Javascript Syntax Decline Everything and Accept Everything ( Practice JavaScript Syntax: Arrays, Loops, Objects, Iterators | Codecademy. I have a line of code that is giving me an unexpected token error and I don’t understand why. The full code and error will follow a brief line of code and error.
The code:
veggies.forEach(veggie) => politelyDecline(veggie);

The Error:

SyntaxError: Unexpected token

Full code:

Full Error:
/home/ccuser/workspace/intermediate-javascript-coding-challenge-using-for-each/main.js:9
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_nod

Any help would be greatly appreciated! I’ve been stuck here for a while. According to the MDN, my code is written with proper syntax.
Thanks,
Mel

Hi,

I think the syntax should be:

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

declineEverything(veggies)

You can also do:

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

declineEverything()

but this version makes less sense, since the function declineEverything() can only decline veggies in this case.

In either case the syntax should be someArray.forEach(item => someFunction(item));.

The syntax was written as such in MDN, maybe it was confused with the arrow function call?

1 Like

Thank you so much! I’ve been pulling my hair out for a week over this!

Big question. This is 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 = (arr) => {

arr.forEach(element => politelyDecline(element));

};

declineEverything(veggies)

My output shows me exactly what I am looking for, but I get the red X and cannot move on. My head hurts on this one.

Update: I figured it out. I was not properly using a callback function. The correct code is as follows:

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

I overthought this one by about 50 miles.

How can you use politely decline without it taking any input?