teacosts
teacosts

Reputation: 25

Making a simple HTML calculator issue in Javascript

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);

Upvotes: 0

Views: 56

Answers (1)

Argee
Argee

Reputation: 1224

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()

Upvotes: 2

Related Questions