加法运算符无法在JavaScript中使用 [英] Addition operator not working in JavaScript

查看:130
本文介绍了加法运算符无法在JavaScript中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加法运算符不适用于Javascript.如果我做5 + 5,它将得到55,而不是10.我该如何解决?

Addition operator isn't working for me in Javascript. If I do 5+5, it gives me 55 instead of 10. How can I fix this?

var numberOne = prompt (Enter first number.);
if (numberOne > 0.00001) {
 var numberTwo = prompt(Enter the second number.);
if (numberTwo > 0.00001) {
var alertAnswer = alert (numberOne + numberTwo);
}
}

推荐答案

您正在阅读字符串,并将其串联.您需要使用parseInt将它们转换为整数.

You're reading in strings, and concatenating them. You need to convert them to integers with parseInt.

IE:

var numberOne = parseInt(prompt("Enter first number."), 10);

这篇关于加法运算符无法在JavaScript中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆