Javascript:" +“符号连接而不是给出变量的总和 [英] Javascript: "+" sign concatenates instead of giving sum of variables

查看:74
本文介绍了Javascript:" +“符号连接而不是给出变量的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个网站,可以帮助我快速回答物理问题。
实际上,代码没有按预期运行,这里是代码

I am currently creating a site that will help me quickly answer physics questions. As it happens, the code didn't run as expected, here is the code

if (option == "dv") {
    var Vinitial = prompt("What is the Velocity Initial?")
    var acceleration = prompt("what is the acceleration?")
    var time = prompt("what is the time?")

    Vfinal = Vinitial + acceleration * time

    displayV.innerHTML = "v= vf= " + Vfinal + "ms" + sup1.sup();
}

现在,让我们说Vinitial是9,加速是2,时间是3 。
当代码运行时,而不是15为Vfinal,我得到96.
我发现它将加速度和时间相乘,然后只是连接9开头,6 (2 * 3的乘积。)

Now, let's say Vinitial was 9, acceleration was 2, and time was 3. When the code runs, instead of getting 15 for "Vfinal", I get 96. I figured out that it multiplies acceleration and time fine, and then just concatenates the 9 at the beginning, with 6 (the product of 2 * 3).

我现在使用

Vfinal =  acceleration * time - (-Vinitial)

避免使用 +签字,但我不想继续这样做。我该如何解决?

which avoids using the "+" sign, but I don't want to have to keep doing this. How do I fix it?

推荐答案


面向对象的JavaScript - 第二版 :正如您所知,当您使用时带有两个数字的加号,这个
是算术加法运算。但是,如果你使用带字符串的加号
符号,这是一个字符串连接操作,并且
返回粘在一起的两个字符串:

Object-Oriented JavaScript - Second Edition: As you already know, when you use the plus sign with two numbers, this is the arithmetic addition operation. However, if you use the plus sign with strings, this is a string concatenation operation, and it returns the two strings glued together:

var s1 = "web";
var s2 = "site";
s1 + s2; // website

+运算符的双重目的是错误来源。因此,如果你打算连接字符串,
,最好确保所有操作数都是字符串的
。这同样适用于添加;
如果你打算添加数字,请确保操作数是数字

The dual purpose of the + operator is a source of errors. Therefore, if you intend to concatenate strings, it's always best to make sure that all of the operands are strings. The same applies for addition; if you intend to add numbers, make sure the operands are numbers.

你可以使用+运算符和 prompt()将返回值从字符串转换为int

You can use "+" operator with prompt() to convert returned values from string to int

var Vinitial = +prompt("What is the Velocity Initial?");
var acceleration = +prompt("what is the acceleration?");
var time = +prompt("what is the time?");

说明:

var a = prompt('Enter a digit'); 
typeof a; // "string"
typeof +a; // "number"

如果输入非数字数据 + a 为您提供 NaN typeof NaN number:)

If you will enter non-digit data +a gives you NaN. typeof NaN is "number" too :)

你将得到与 parseInt()相同的结果:

You will get the same result with parseInt():

var Vinitial = parseInt(prompt("What is the Velocity Initial?"), 10);
var acceleration = parseInt(prompt("what is the acceleration?"), 10);
var time = parseInt(prompt("what is the time?"), 10);




developer.mozilla.org parseInt(string,radix);


  • string :要解析的值。

radix :2到36之间的整数,表示上述字符串的基数(数学数字系统中的基数)。
为人类常用的十进制数字系统指定10。
始终指定此参数以消除读者混淆,并且
保证可预测的行为。当未指定基数时,不同的实现产生
不同的结果,通常将
的值默认为10.

radix: An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

结语:


面向对象的JavaScript - 第二版 :最安全的事情要做的是始终指定基数。如果你省略基数,你的代码
可能仍然可以在99%的情况下工作(因为大多数情况下你通常用
来解析小数),但每隔一段时间它就会导致你
位在调试一些边缘情况时脱发。例如,假设
您有一个表单字段接受日历日或月,而
用户表示06或08。

Object-Oriented JavaScript - Second Edition: The safest thing to do is to always specify the radix. If you omit the radix, your code will probably still work in 99 percent of cases (because most often you parse decimals), but every once in a while it might cause you a bit of hair loss while debugging some edge cases. For example, imagine you have a form field that accepts calendar days or months and the user types 06 or 08.

Epilogue II:


ECMAScript 5删除八进制文字值并避免混淆
使用parseInt()和未指定的基数。

ECMAScript 5 removes the octal literal values and avoids the confusion with parseInt() and unspecified radix.

这篇关于Javascript:" +“符号连接而不是给出变量的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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