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

181
Views
The code using Arrow function dosenot work to calculate average

So I have written this code to calculate the average marks of students using Javascript. But for some reason it just gives the output as 242628302.6 always. I dont understand whats wrong with the code....Any help would be appreciated.

const marks = [];
var size = 5; //Maximum Array size

for (var i = 0; i < size; i++) { //Taking Input from user
   marks[i] = prompt('Enter Element ' + (i + 1));
}
const average = marks => marks.reduce((prev, curr) => prev + curr, 0) / marks.length;
const result = average(marks);
console.log(result);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try with Number :

const marks = [];
var size = 5; //Maximum Array size

for (var i = 0; i < size; i++) { //Taking Input from user
     marks[i] = prompt('Enter Element ' + (i + 1));
}
const average = marks => marks.reduce((prev, curr) => Number(prev) + Number(curr), 0) / marks.length;
const result = average(marks);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You need to convert the data type from string to Number since the prompt method return type is a string.

const marks = [];
                var size = 5; //Maximum Array size

                for (var i = 0; i < size; i++) { //Taking Input from user
                    marks[i] = prompt('Enter Element ' + (i + 1));
                }
                const average = marks => marks.reduce((prev, curr) => prev + Number(curr), 0) / marks.length;
                const result = average(marks);
                console.log(result);

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!