Validated score average
Review scores arrive as tokens. Accept only whole decimal integers from 0 through 100 inclusive. Print the average of accepted scores to one decimal place and the number rejected; if no score is accepted, print NONE. Values such as 88.5, NaN and 101 are invalid.
Input
One line of space-separated score tokens.
Output
Average or NONE, then rejected X on the next line.
Example 1
Input
100 80 bad 88.5 0
Output
60.0 rejected 2
Scores 100, 80 and 0 average to 60.0; the word and decimal token are rejected instead of coerced.
Constraints
- The input contains 1 to 200 score tokens.
- Only whole decimal integer tokens from 0 through 100 are accepted.
Hints
Hint 1 of 2
Number() alone accepts forms you may not want; check the token's syntax too.
Hint 2 of 2
Avoid division when count is zero.
Solution
Show a reference solution and explanation
const tokens = readline().split(' ');
let total = 0, count = 0, rejected = 0;
for (const token of tokens) {
const value = Number(token);
if (!/^\d+$/.test(token) || !Number.isSafeInteger(value) || value > 100) rejected++;
else { total += value; count++; }
}
console.log(count ? (total / count).toFixed(1) : 'NONE');
console.log('rejected', rejected);
Why it works
A decimal-token check excludes floats and non-numeric forms before range validation. The accumulator changes only for accepted values, and the rejected counter accounts for every other token. toFixed(1) produces a stable display format.
Lesson for this exercise: Error Handling in JavaScript