Exercise · Operators and type conversion · lesson 14 of 14
Exercise: a number or a fallback
Write a function function toNumberOr(text, fallback) that returns Number(text), or the value fallback if that is NaN.
toNumberOr("42", 0)→42toNumberOr("12px", -1)→-1toNumberOr("0", 7)→0
Constraints: text is a string, possibly empty (it gives 0, as in Number); fallback is any number, including 0.
Do not add extra imports unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.