Kaiyang
Kaiyang

Reputation: 1

Calculating average with prompt of an number

How can I make a function to find average from a number insert from prompt? Instead of an array of data.

It is different form all the tutorial on the site because the tutorial and solution here are having a set of data already but instead this question above wants us to calculate the sum of the input number form the prompt.

Upvotes: 0

Views: 316

Answers (1)

michal pavlik
michal pavlik

Reputation: 348

for example:

const size = parseInt(prompt("insert number")) + 1;
if(size > 0){
  const arr = [...Array(size).keys()];
  const sum = arr.reduce(function(a, b) { return a + b; }, 0);
  alert(sum / size );
}

Upvotes: 1

Related Questions