0

just a simple code for calculating average age of 'adult' array using reduce method.

 const ageAvg = adult.reduce((acc, age) => {
    acc + age;
  }, 0);
  console.log(ageAvg / adult.length);

im having a hard time figuring out what is happening behind the scene when i use curly braces for my arrow function content as in the example, cause when i do, ageAvg gives me NaN and when i dont use any braces at all for the acc + age it works totally fine. i guess it has something to do with the 'this' keyword but i would like to understand exactly whats going on so

  • "i guess it has something to do with the 'this' keyword" — What makes you think it has something to do with a keyword **you aren't using**? – Quentin Dec 05 '22 at 16:13
  • Curly braces require an explicit return. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body – Dave Newton Dec 05 '22 at 16:13
  • Dave Thanks for the helpful comment. i got it – Dan Yanuka Dec 06 '22 at 13:48

0 Answers0