简体   繁体   English

为什么较大的变量小于较小的变量

[英]Why is a bigger variable less than a smaller variable

Summary: 摘要:
I have a code that takes words from a database, it puts it all in a box which has 3 by 3 text boxes (you can also expand instead of using 3 boxes, which I confirmed worked). 我有一个从数据库中提取单词的代码,将其全部放入一个具有3 x 3文本框的框(您也可以扩展而不是使用3框,这我确认是可行的)。

Inside the boxes, it takes the height of the words and sets that height to the height of the div holding the words, then places that height inside of a array. 在框内,它获取单词的高度并将该高度设置为保存单词的div的高度,然后将该高度放置在数组内。 If a div is not there (no text), it places a 0. 如果div不存在(无文本),则放置0。

Every 3rd time it does this, which is 1 row, it will check the height of the three in the row, and set the div above them as the biggest height, like so: 每执行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

Now, the way my page is, temp will be: 现在,我的页面的方式是,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

The Problem: 问题:
when my code gets to Id[Ten] is 1, It will start by saying: 当我的代码到达Id [Ten]为1时,它将开始于:
"284 is greater than 0" “ 284大于0”
which is planned, then I get: 这是计划好的,然后我得到:
"284 284" “ 284 284”
which is also planned, then: 也已计划,然后:
"50 is greater than 284" “ 50大于284”
Here is the problem: what came after that is what should have been skipped: 这是问题所在:此后应该跳过的内容是:
"50 50" “ 50 50”
This tells me that the if statement I have passed True once it saw 284 < 50. I have done math at least more than once in my life, and I know 284 is a bigger number than 50. Due to this, my box has a smaller height than the text in it, and they overlap in an way uglier than me. 这告诉我,如果if语句看到284 <50,则通过了True。我一生中至少做过一次数学运算,并且我知道284比50大。因此,我的盒子有一个高度比其中的文字小,并且它们的重叠程度比我小。 I'm sure my code and I are having some kind of mid-life crisis, and would be great if someone can explain to me why my code is going through this rebellious stage. 我确定我的代码和我正处于某种中年危机,如果有人可以向我解释我的代码为何要经历这个叛逆阶段,那将是非常不错的。

This is caused because you are comparing string to string, when comparing string js verify only your alphabetical order. 这是因为您正在比较字符串与字符串,所以在比较字符串js时仅验证字母顺序。

You have to use parseInt in your variables. 您必须在变量中使用parseInt。

This is the test I did in the chrome console. 这是我在chrome控制台中进行的测试。

var test1 = 50
var test2 = 200;

test1 > test2
false

--------

var test1 = "50"
var test2 = "200";

test1 > test2
true

parseInt(test1) > parseInt(test2)
false

Your code is comparing strings (alphabetical order): 您的代码正在比较字符串(字母顺序):

"Zoo" > "Apple" // true
"50" > "284" // true

You need to compare numbers by using something like parseInt: 您需要使用诸如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> 

In javascript everything is an object.It does not has any type It determines type by the way we declare it Here you are comparing (tempN < temp[ix]) tempN and temp[ix] both are strings you have to convert them to numbers and then compare 在javascript中,一切都是对象。它没有任何类型,它通过我们声明的方式确定类型。在这里,您正在比较(tempN <temp [ix])tempN和temp [ix]都是字符串,您必须将它们转换为数字然后比较

   var number1=parseInt(tempN,10);
    var number2=parseInt(temp[i-x],10);
     console.log(number1<number2);

If you want to convert array of numbers to get sorted but not alphabetically you can do something like this 如果要转换数字数组以进行排序但不按字母顺序排列,可以执行以下操作

var numbers=[1,90,5,8];
numbers.sort(SortCompare);
function SortCompare(a,b){
return a-b;
}

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM