简体   繁体   English

大于会在数字小于100时返回错误的值。Javascript

[英]Greater than returns wrong value on numbers lower then 100. Javascript

This is my first javascript project, so I'm sure this code isn't pretty, and could be written in a much better way, but that aside, I've encountered a problem I just don't understand. 这是我的第一个javascript项目,因此我确定这段代码不是很漂亮,并且可以用更好的方式编写,但是除此之外,我遇到了一个我不明白的问题。 I'm sure it's just a bug I've made myself, but I just simply can't find it. 我确定这只是我自己做的一个错误,但我只是找不到它。

The '>' (greater than) operator works fine on numbers over 100, but stops working when it gets to 100. For some reason 100 > 99 returns false? '>'(大于)运算符可以很好地处理超过100的数字,但是在达到100时会停止工作。出于某种原因,100> 99返回false吗?

A link to a jsbin. 到jsbin的链接。 Move the slider to the right, and then slowly to the left, and you will see it returning "true" until it reaches 100. From there it returns "false" 向右移动滑块,然后向左缓慢移动,您将看到它返回“ true”,直到达到100。从那里返回“ false”

https://jsbin.com/vigocu/edit?console,output https://jsbin.com/vigocu/edit?console,output

function getSliderInput(inputSliderId) {
  var backSwingArray = [100];
  var downSwingArray = [];


  document.querySelector('#' + inputSliderId).addEventListener('input', fillArray , false);

function fillArray() {

  if (isNaN(downSwingArray[downSwingArray.length - 1]) && backSwingArray[backSwingArray.length - 1] < this.value) {
    backSwingArray.push(this.value);
  } else if (downSwingArray[downSwingArray.length - 1] > this.value || isNaN(downSwingArray[downSwingArray.length - 1])){
    console.log('Is ' + downSwingArray[downSwingArray.length - 1] + ' > ' + this.value + ' return ' + (downSwingArray[downSwingArray.length - 1] > this.value));
    downSwingArray.push(this.value);
  } else {
    console.log('Is ' + downSwingArray[downSwingArray.length - 1] + ' > ' + this.value + ' return ' + (downSwingArray[downSwingArray.length - 1] > this.value));
    return;
  }
}
}

value on input elements is always a string . valueinput元素始终是一个字符串 While that won't be a problem initially, when you're comparing this.value to the 100 you've put in the array to start with, you then push this.value into the array as-is (as a string). 虽然最初不会出现问题,但是当您将this.value与数组中放入的100进行比较时,您可以按原样(作为字符串)将this.value推入数组。 That means later, you'll end up comparing that stored string with another this.value value, which is also a string. 这意味着以后,您将比较该存储的字符串与另一个this.value值,该值也是一个字符串。 If either operand to > is a number, it will coerce the other operand to number (the way + does, see below), but if both operands are strings, it will do a lexical comparison, not a numeric one, and "100" is indeed < "99" because "1" is < "9" . 如果>任何一个操作数都是数字,它将把另一个操作数强制转换为数字( +的方式,请参见下文),但是如果两个操作数都是字符串,它将进行词法比较,而不是数字的"100"实际上是< "99"因为"1"< "9"

So you want to convert this.value to a number early on, and then use that number both when comparing and when pushing into your array. 因此,您想尽早将this.value转换为数字,然后在比较和推入数组时都使用该数字。 You have many ways to do that: 您有很多方法可以做到这一点:

  • The unary + will require the entire string to be a valid number, but will treat "" as 0 ; 一元+将要求整个字符串为有效数字,但会将""视为0 it will also treat strings starting with 0x as hexadecimal 还将以0x字符串视为十六进制

     var num = +this.value; // or perhaps var num = this.value === "" ? NaN : +this.value; // or even var str = this.value.trim(); // .trim can be shimmed on obsolete browsers var num = str === "" ? NaN : +str; 
  • parseInt(..., 10) (the 10 is specifying the radix [number base] to use) will allow garbage at the end of the string, treat "" as NaN , and treat any string prefixed with 0x as 0 (since it stops at the first invalid character) parseInt(..., 10) (10指定要使用的基数[数字基])将允许在字符串末尾进行垃圾处理,将""视为NaN ,并将任何以0x字符串视为0 (因为它在第一个无效字符处停止)

     var num = parseInt(this.value, 10); 
  • Number(...) does what + does Number(...)+

     var num = Number(this.value); // or variations above on + 

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

相关问题 使用 javascript,如何将值设置为以小数点结尾的数字输入(即“100.”) - Using javascript, how to set a value to a number input that ends in decimal dot (ie. “100.”) 指令接受大于0且小于100的数字 - Directive to accept numbers greater than 0 and less than 100 正则表达式允许十进制数大于0且小于100 - Regex to allow decimal numbers greater than 0 and less than 100 如果 window 宽度小于给定值,我如何使用 JavaScript 隐藏 Html 中的文本,并在大于该值时显示它 - How can i hide a text in Html using JavaScript if the window width is lower than a given value and show it if it's greater than the value RegEx在Javascript中替换大于变量的数字 - RegEx replacing numbers greater than variable in Javascript 如何在JavaScript中找到一个小于108且大于0的值:脚本中出现错误 - How to find that a value is less than 108 and greater than 0 in JavaScript: something wrong in script javascript div长度,如果值大于 - javascript div length if value greater than javascript测试值是否为数字和大于0的数字 - javascript testing if a value is a number AND a number greater than 0 生成m个等于100但两个数字之差必须大于4的数字 - Generating m numbers that equal 100 but difference between two numbers have to be greater than 4 (大于和小于)或相同的严格形式 - (greater than and lower than) or equal strict forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM