Read the question, write JavaScript on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
The first line is n. The second line has n integers. Log their average, rounded to exactly two decimals.
reduce() folds an array down to a single value — here, the sum.
Input. Line 1: n. Line 2: n integers separated by spaces.
Output. The average with two decimals, e.g. 5.00.
Constraints
- 1 ≤ n ≤ 1000
Examples
Input
4 2 4 6 8
Output
5.00
Input
3 1 2 2
Output
1.67
Hint
- nums.reduce((s, x) => s + x, 0) sums the array.
- Divide by n, then format with .toFixed(2).
Show correct code
Peek only after you have tried. You can still Check your own version.
const n = Number(readLine());
const nums = readLine().trim().split(/\s+/).map(Number);
console.log((nums.reduce((s, x) => s + x, 0) / n).toFixed(2));
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.