JavaScript · Exercise 5 of 5 Previous
Intermediate
Fix a broken calculator
This calculator adds the two boxes when you press Add. Right now 2 and 3 give 23. Fix it so it adds numbers: 2 and 3 must show 5, and 1.5 and 2.25 must show 3.75.
If either box is empty, blank, or not a number (like abc), show exactly Please enter two numbers instead.
Remember: input.value is always a string, and Number("") is 0, not NaN.
What Check tests
- 2 + 3 shows 5
- 1.5 + 2.25 shows 3.75
- Spaces around a number are fine:
-2+7shows 5 abcshows the error message- An empty box shows the error message
- A blank box (only spaces) shows the error message
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.