Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem 5. Min, Max, Sum and Average of N Numbers
- //
- // Write a program that reads from the console a sequence of n integer numbers and returns the minimal, the maximal number,
- // the sum and the average of all numbers (displayed with 2 digits after the decimal point).
- // The input starts by the number n (alone in a line) followed by n lines, each holding an integer number.
- // The output is like in the examples below.
- //
- // Examples:
- // n output
- // 3 min = 1
- // 2 max = 5
- // 5 sum = 8
- // 1 avg = 2.66
- let input = ['3',
- '2',
- '5',
- '1'
- ];
- let numCount = Number(gets());
- let arr = [];
- let numMin = Number.MAX_SAFE_INTEGER;
- let numMax = Number.MIN_SAFE_INTEGER
- // loop with length of numCount
- for (let i = 0; i < numCount; i++) {
- arr.push(+gets());
- }
- for (let i = 0; i < arr.length; i++){
- if (numMin > arr[i]) {
- numMin = arr[i].toFixed(2);
- }
- }for (let i = 0; i < arr.length; i++){
- if (numMax < arr[i]) {
- numMax = arr[i].toFixed(2);
- }
- }
- let sum = 0;
- for( let i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- sum = sum.toFixed(2);
- let numAvg = (sum / numCount).toFixed(2)
- console.log(`min=${numMin}
- max=${numMax}
- sum=${sum}
- avg=${numAvg}`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement