Clean Up the Inputs
JavaScript
20 pts
Clean Up the Inputs
People typed weights into a form, and some typed nonsense. Write:
toNumberOrZero(text): converts the text withNumber(); if the result isNaN, it returns0instead.sumTexts(texts): returns the sum of all values of the array, each converted withtoNumberOrZero(usereduceor a loop).
Print the converted array (use map) and the sum. Expected output:
[ 12, 0, 7.5, 0, 0 ] 19.5
Checklist
toNumberOrZeroconverts text to a number…- …and gives 0 instead of NaN.
sumTextsadds up all converted values (0 for an empty array).- Print the converted array and the sum.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
