-5

Using the forEach() method to loop over the array and print out the following donut summaries using console.log.

Jelly donuts cost $1.22 each

Chocolate donuts cost $2.45 each

Cider donuts cost $1.59 each

Boston Cream donuts cost $5.99 each

var donuts = [
  { type: "Jelly", cost: 1.22 },
  { type: "Chocolate", cost: 2.45 },
  { type: "Cider", cost: 1.59 },
  { type: "Boston Cream", cost: 5.99 }
];
SAYE
  • 1,247
  • 2
  • 20
  • 47
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Kevin Boucher May 31 '18 at 15:13
  • I'm voting to close this question as off-topic because this is not a *i just dump my homework here so that soneone else solves it* site. We want to help you. Helping means that we require you to do the work we "just" help when you get stuck. – Jonas Wilms May 31 '18 at 15:13
  • Did you tried out something on your own to achieve what you want ? If so it might be relevant to include it in your post. – Martin May 31 '18 at 15:14

2 Answers2

-3

The forEach() method executes the callback function once per array item.

Set the callback of the forEach() loop to console log the desired values, you can access them with the argument passed (here e).

var donuts = [
  { type: "Jelly", cost: 1.22 },
  { type: "Chocolate", cost: 2.45 },
  { type: "Cider", cost: 1.59 },
  { type: "Boston Cream", cost: 5.99 }
];

donuts.forEach(e=> console.log(`
  ${e.type} donuts costs ${e.cost} each
`))
Ivan
  • 34,531
  • 8
  • 55
  • 100
  • 3
    I'd imagine it's the "this is a stupid question so no one should answer it" police. – Liam May 31 '18 at 15:16
  • Well, downvote the question then, not the answer, that's nonsense. No rules said I was not allowed to answer questions. Even if they were obvious/duplicates/homework questions. – Ivan May 31 '18 at 15:17
  • 2
    still `+20 - 8 = 12` – Liam May 31 '18 at 15:19
  • Someone is on the roll this morning downvoting a lot of other questions, answers, and comments. – Esaith May 31 '18 at 15:44
  • Are you asking me if I downvoted? Because I didn't – Liam May 31 '18 at 15:57
  • 1
    Do you really think that you help someone with this? I mean he might now the answer now (and can show it to his teacher whatever) but he will get stuck the next time, so on the long term, you actually helped him learning nothing. – Jonas Wilms May 31 '18 at 16:51
  • @esaith thats not because someones moody but because there were a lot of bad questions today. – Jonas Wilms May 31 '18 at 16:52
  • 1
    @Ivan it is (maybe not a traditional school homework, but its still a task assigned to him so that he learns from it), and if you would've added a nice explanation of how `forEach` works and so on and gave some hints i would've upvoted. However you added a snippet doing "his task" and after receiving downvotes you added an explanation – Jonas Wilms May 31 '18 at 17:00
-4

Check the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

var donuts = [
  { type: "Jelly", cost: 1.22 },
  { type: "Chocolate", cost: 2.45 },
  { type: "Cider", cost: 1.59 },
  { type: "Boston Cream", cost: 5.99 }
];

donuts.forEach(function(element) {
    console.log(element);
});
Dennis de Best
  • 1,078
  • 13
  • 30
  • Thanks for your honest feedback. This is not a homework. Its one of the quiz from my Google Challenge Scholarship on UDACITY. I find it hard to understand even after reading the MDN. – Ayoola Daniel May 31 '18 at 15:18
  • 1
    I just figured it out. Using this! donuts.forEach(function(donuts){ console.log(donuts.type + ' donuts cost ' + '$' + donuts.cost + ' each'); }); – Ayoola Daniel May 31 '18 at 16:18