Validated score average
A results export contains space-separated tokens; damaged scores appear as text. Ignore tokens that are not finite numbers. Print the average of valid scores to two decimals, or NO DATA when none are valid.
Input
One line containing 1 to 100 space-separated tokens.
Output
The valid-score average with two decimal places, or NO DATA.
Example 1
Input
10 bad 20 15x 30
Output
20.00
Only 10, 20 and 30 pass strict numeric validation.
Constraints
- Valid scores are between -10000 and 10000
- Invalid tokens contain no whitespace
Hints
Hint 1 of 3
Number(token) performs a complete conversion unlike parseFloat.
Hint 2 of 3
Number.isFinite rejects NaN and infinities.
Hint 3 of 3
Check values.length before reducing and dividing.
Solution
Show a reference solution and explanation
const tokens = readline().split(' ');
const values = [];
for (const token of tokens) {
const value = Number(token);
if (token !== '' && Number.isFinite(value)) values.push(value);
}
if (values.length === 0) console.log('NO DATA');
else console.log((values.reduce((sum, value) => sum + value, 0) / values.length).toFixed(2));
Why it works
Strict conversion rejects partial values such as 15x, which parseFloat would incorrectly accept as 15. The empty-array branch prevents invalid reduction or division and gives the requested explicit result.
Lesson for this exercise: Error Handling in JavaScript