Skip to content
Advertisement

Problem with script that is suppose to add two numbers

I wrote up a code designed to add two numbers and it keeps returning a NaN when I ask for an answer, I am fairly new but would like to know why this code in particular does not work so I can make sure I don’t make the mistake again.

HTML

JavaScript

JavaScript

JavaScript

So the first condition works and sends an alert box asking to input two numbers, the second condition returns a an Alert box with NaN instead of adding the two numbers

css

JavaScript

Advertisement

Answer

Take out the number after you click on the button not before. Everything else is great.

TIP: As you are adding the number there must be always a type number so it would be better to add type="number" on input so that the user cannot enter alphabets or special characters.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement