0

I'm working on some ES6 code, and I want to console.log or document.write the answer to my function. When I attempt to do this, it writes the entire function instead. Is there an easy way to do this?

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

const half = ({max, min}) => (max+min) / 2.0; 

document.write(half)
  • 2
    __Call__ the function. – tkausl Oct 12 '21 at 18:58
  • 1
    `half` is a function; you haven't invoked it. Are you familiar with JavaScript's invocation syntax? `half(stats)`, perhaps? (Tangential; why are you using `document.write()`? This has not been considered best practice for quite some time now.) – esqew Oct 12 '21 at 18:58
  • 1
    Use `()` to invoke function. – Hassan Imam Oct 12 '21 at 18:58

1 Answers1

3

You just need to invoke the function using parenthesis (and pass the stats object as a parameter):

const stats = {
 max: 56.78,
 standard_deviation: 4.34,
 median: 34.54,
 mode: 23.87,
 min: -0.75,
 average: 35.85
};

const half = ({max, min}) => (max+min) / 2.0; 

document.write(half(stats))
silencedogood
  • 3,209
  • 1
  • 11
  • 36