JavaScript if Statement .length 不起作用。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24237355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:25:50  来源:igfitidea点击:

JavaScript if Statement .length not working. Why?

javascripthtmlcssif-statementconditional-statements

提问by user3295521

I have this if statement i have came up with here:

我有我在这里提出的 if 语句:

var TotalMoney=0;
var Orbs=0;
if (TotalMoney.length==2) {
Orbs+=1;
}

What this code is supposed to do is if the the "TotalMoney" value digit length equals 2, example (the number 10 has 2 digits) then it will add 1 "Orb" to the "Orbs" value. Currently, it does nothing. There is HTML and CSS linked to this code but i figured the problem is in this code as it works fine for everything else. Please fix it as i have been trying for hours. Thanks!

这段代码应该做的是,如果“TotalMoney”值的数字长度等于 2,例如(数字 10 有 2 位数字),那么它将向“Orbs”值添加 1 个“Orb”。目前,它什么都不做。有 HTML 和 CSS 链接到此代码,但我认为问题出在此代码中,因为它适用于其他所有内容。请修复它,因为我已经尝试了几个小时。谢谢!

For my second question that i just found out with this code here:

对于我刚刚在此处使用此代码发现的第二个问题:

var totalMoney=0;
var orbs=0;
if (totalMoney.toString().length==2) {
orbs+=1;
}

This works on getting the number value digits as 2 digits long. The problem now is that once it reaches 10, every time that number goes up (10-99) all the way up, it will add 1 orb each time. I only want it to add 1 orb only when it gets to the 2 digit number (10) and stops adding 1 orb after it reaches it. How can i achieve this? Thanks!

这适用于将数值位数设为 2 位数。现在的问题是,一旦达到 10,每次该数字一路上升(10-99)时,每次都会增加 1 个球。我只希望它仅在达到 2 位数字 (10) 时才添加 1 个球体,并在到达后停止添加 1 个球体。我怎样才能做到这一点?谢谢!

回答by Scimonster

TotalMoneyis a number, so it doesn't have a lengthproperty. You can check the length of the number by first converting to a string: TotalMoney.toString().length.

TotalMoney是一个数字,所以它没有length属性。您可以通过首先转换为字符串来检查数字的长度:TotalMoney.toString().length

回答by TiMirLAN

Number object in js has no length property, so TotalMoney.lengthreturn undefined.

js 中的 Number 对象没有 length 属性,所以TotalMoney.length返回undefined.

If you want count digits you may use this:

如果你想计算数字,你可以使用这个:

if (TotalMoney.toString().length == 2) {
    Orbs+=1;
}

But if TotalMoneywill be negative, -1 for exmple, Orbs wil be incremented.

但是如果TotalMoney是负数,例如 -1,Orbs 将增加。

I think there are better way to find all 2-digits number:

我认为有更好的方法来查找所有 2 位数字:

if (TotalMoney>9 && TotalMoney<100) {
    Orbs+=1;
}

回答by AmanS

TotalMoneyis numeric so to find its length use this code

TotalMoney是数字所以要找到它的长度使用这个代码

TotalMoney.toString().length;

Instead of

代替

TotalMoney.length; 

so try to modify your code as below:

所以尝试修改你的代码如下:

var TotalMoney=0;
var Orbs=0;
if (TotalMoney.toString().length==2) {
    Orbs+=1;
}

回答by LexJacobs

here are some ideas and general comments on your code.

以下是对您的代码的一些想法和一般评论。

// recommended to start with lower case. upper case such as 'TotalMoney'
// is stylistically reserved for constructors.
var totalMoney=0; 

// again - changing form Orbs to orbs;
var orbs=0;

// recommended to use '===' until you are more experienced with JavaScript and
// know about the 'gotchas' that '==' might produce.

// you will be able to check the length of totalMoney only after converting it to a string.
if (totalMoney.toString().length === 2) {
orbs+=1;
}

Finally, totalMoney of 0 will not add one to orbs. But totalMoney of 10, as you mentioned, will.

最后,totalMoney 为 0 不会向球体添加 1。但是正如你提到的,totalMoney 10 会。

回答by Ninad

Length is property of array & string.It can not be applied on other variables.

长度是数组和字符串的属性。它不能应用于其他变量。

If you want to count number of digits you can do

如果你想计算你可以做的位数

if(TotalMoney>9)

Or you can convert it to string then check it's length

或者您可以将其转换为字符串然后检查它的长度

if(TotalMoney.toSting().length>2)