Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How do I calculateAverage?

I need to create a function in Javascript that is called calculateAverage which accepts arrays of numbers. The function should return the average in the array. If the array is empty, return 0.

I am new to coding and have only studied python.

int calculateAverage(int numbers_count, int* numbers) {
calculateAverage([50, 21, 80])
calculateAverage([30, 39, 58, 11])
calculateAverage([200])
calculateAverage([])
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

this can be a solution:

 function calculateAverage(arr) {
  const sum = arr.reduce((acc, curr) => acc + curr, 0)
  return sum / arr.length
}

const arr = [5,10,1,5,20,50,30,25];

const average = calculateAverage(arr);
console.log(average)
about 4 years ago · Juan Pablo Isaza Report

0

you can do that like this...

function calculateAverage(arr) {
  const rslt = arr.reduce((a, b) => a + b, 0) / arr.length;
  return 0 === ~~rslt ? 0 : rslt;
}

console.log(calculateAverage([30, 39, 58, 11]));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!