0

I am learning Javascript and I am trying to make a simple HTML calculator where 2 prompts appear asking for numbers to add. Anytime I execute I get just the 2 numbers added together like 2 + 2 = 22, I did something wrong because I would want the response to be the right answer. Thanks so much!

'use strict';

let txt = prompt('Please enter your calculation');
let cal = prompt('And the other one');

let orange = txt; let apple = cal;

alert(orange + apple);
1
  • You need to convert into integer or float as they are strings Commented May 8, 2020 at 10:13

1 Answer 1

2

Your prompt inputs (txt and cal) are strings... you need to parse them to integers

let txt = prompt('Please enter your calculation');
let cal = prompt('And the other one');
let orange = parseInt(txt); let apple = parseInt(cal);

alert(orange + apple);

if you also want to add floats, use parseFloat() instead of parseInt()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.