简体   繁体   English

在 Javascript 中制作一个简单的 HTML 计算器问题

[英]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.我正在学习 Javascript 并且我正在尝试制作一个简单的 HTML 计算器,其中出现 2 个提示要求添加数字。 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.每当我执行时,我只会将 2 个数字加在一起,例如 2 + 2 = 22,我做错了,因为我希望响应是正确的答案。 Thanks so much!非常感谢!

'use strict'; '使用严格';

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

let orange = txt;让橙色 = txt; let apple = cal;让苹果 = 卡尔;

alert(orange + apple);

Your prompt inputs (txt and cal) are strings... you need to parse them to integers您的提示输入(txt 和 cal)是字符串...您需要将它们解析为整数

 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()如果您还想添加浮点数,请使用 parseFloat() 而不是 parseInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM