[英]Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?
[英]Why is a bigger variable less than a smaller variable
摘要:
我有一个从数据库中提取单词的代码,将其全部放入一个具有3 x 3文本框的框(您也可以扩展而不是使用3框,这我确认是可行的)。
在框内,它获取单词的高度并将该高度设置为保存单词的div的高度,然后将该高度放置在数组内。 如果div不存在(无文本),则放置0。
每执行3次,即1行,它将检查该行中三个的高度,并将其上方的div设置为最大高度,如下所示:
var tempN = 0; //Holds each number temporarily
var temp = []; //Holds all 9 box heights
for (var i = 0; i < Panels.length; i++) { //There are 9 panels
temp[i] = new Array(); //adding to array
if (document.getElementById('Column'+i+Id[Ten])!=null) {//Id[Ten] will be 3, 2, and 1, in a for loop
document.getElementById('Column'+i+Id[Ten]).style.height = document.getElementById('Text'+i+Id[Ten]).clientHeight+"px";
temp[i].push(document.getElementById('Column'+i+Id[Ten]).clientHeight); //puts number on table
} else {
temp[i].push(0); //since there is no panel, the height would be 0
}
if (i%3 == 2) { //every 3rd time
for (var x=2;x >= 0;x--) { //also could do (var x=0;x<3;x++)
alert(temp[i-x]+" is greater than "+tempN); //tells me what should work
if (tempN < temp[i-x]) { //also could do the other way around
tempN = temp[i-x]; //tempN will be the current biggest height
alert(tempN+' '+temp[i-x]); //just a show it went through if
document.getElementById('Row'+BoxRow+Id[Ten]).style.height = tempN + "px";
}
}
tempN = 0; //reset tempN for next 3 boxes
}
}
temp = []; //reset temp for next box
现在,我的页面的方式是,temp将是:
temp = [0,158,0,0,158,0,0,50,0] when Id[Ten] is 3
temp = [0,50,0,0,0,0,0,0,0] when Id[Ten] is 2
temp = [284,50,50,50,50,50] when Id[Ten] is 1
问题:
当我的代码到达Id [Ten]为1时,它将开始于:
“ 284大于0”
这是计划好的,然后我得到:
“ 284 284”
也已计划,然后:
“ 50大于284”
这是问题所在:此后应该跳过的内容是:
“ 50 50”
这告诉我,如果if语句看到284 <50,则通过了True。我一生中至少做过一次数学运算,并且我知道284比50大。因此,我的盒子有一个高度比其中的文字小,并且它们的重叠程度比我小。 我确定我的代码和我正处于某种中年危机,如果有人可以向我解释我的代码为何要经历这个叛逆阶段,那将是非常不错的。
这是因为您正在比较字符串与字符串,所以在比较字符串js时仅验证字母顺序。
您必须在变量中使用parseInt。
这是我在chrome控制台中进行的测试。
var test1 = 50
var test2 = 200;
test1 > test2
false
--------
var test1 = "50"
var test2 = "200";
test1 > test2
true
parseInt(test1) > parseInt(test2)
false
您的代码正在比较字符串(字母顺序):
"Zoo" > "Apple" // true
"50" > "284" // true
您需要使用诸如parseInt之类的数字进行比较:
parseInt("50") > parseInt("284"); // false
var stringResult = "50" > "284"; // true var intResult = parseInt("50") > parseInt("284"); // false document.getElementById('strings').textContent = stringResult; document.getElementById('ints').textContent = intResult;
<h3>Result of "50" > "284" = <span id="strings"></span> </h3> <h3> Result of parseInt("50") > parseInt("284") = <span id="ints"></span> </h3>
在javascript中,一切都是对象。它没有任何类型,它通过我们声明的方式确定类型。在这里,您正在比较(tempN <temp [ix])tempN和temp [ix]都是字符串,您必须将它们转换为数字然后比较
var number1=parseInt(tempN,10);
var number2=parseInt(temp[i-x],10);
console.log(number1<number2);
如果要转换数字数组以进行排序但不按字母顺序排列,可以执行以下操作
var numbers=[1,90,5,8];
numbers.sort(SortCompare);
function SortCompare(a,b){
return a-b;
}
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.