Compute three class statistics from a list of scores: the highest, the lowest, and the average.
You start with:
const scores: number[] = [92, 78, 85, 67, 74, 88, 91, 58];
Build:
Highest: 92 Lowest: 58 Average: 79
Rules:
highestis the largest value inscores.lowestis the smallest value inscores.averageis the sum of all scores divided by how many there are, rounded down (Math.floor(total / scores.length)).- Print
"Highest: ","Lowest: "and"Average: "on three lines, in the order of the sample output (console.log("Highest: " + highest);).
Math.max(...scores) spreads the array into Math.max's arguments. Add the scores up in a loop, then use Math.floor for the integer average.
const scores: number[] = [92, 78, 85, 67, 74, 88, 91, 58];
const highest: number = Math.max(...scores);
const lowest: number = Math.min(...scores);
let total: number = 0;
for (const s of scores) total += s;
const average: number = Math.floor(total / scores.length);
console.log("Highest: " + highest);
console.log("Lowest: " + lowest);
console.log("Average: " + average);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
