2011-07-08 14 views
0

id値が ".somedivs"で、条件文でソートされた後、コンソールにグローバル変数に格納されるシンプルなコードです。下に私はいくつかの奇妙な結果が出ていることがわかります。条件の組み合わせが何であっても、正しく動作するようにはできません。とても基本的なようですが、私はjsの法則に違反していると思っています。Javascript上位変数と下位変数条件付き奇数コンソール出力

$(document).ready(function(){ 

    $('.somedivs').mousedown(function(){ 
     first = $(this).attr("id"); 
    }).mouseup(function(){ 
     second = $(this).attr("id"); 

     if(window.first > window.second){ 
     var higher = window.first; 
     var lower = window.second; 
     var process = 1; 
     }else if(window.second > window.first){ 
     var higher = window.second; 
     var lower = window.first; 
     var process = 2; 
     }else if(window.first === window.second){ 
     var higher = window.first; 
     var lower = window.second; 
     var process = 3; 
     } 


     if(higher > lower){ 
     var status = true; 
     }else{ 
     var status = false; 
     } 

     console.log("process = " + process + " // " + "window.first = "+window.first+"/window.second = "+window.second + " // higher = "+higher+"/lower = "+lower + " // status = "+ status ); 


    }); 

コンソール出力は、ほとんどの行がfalseである必要がある場合にtrueを返します。私は矢印 ">"に間違っているものの印を付けました。 ステータスが偽である必要があるのはなぜですか?

process = 2 // window.first = 0/window.second = 1 // higher = 1/lower = 0 // status = true 
process = 3 // window.first = 2/window.second = 2 // higher = 2/lower = 2 // status = false 
process = 1 // window.first = 2/window.second = 0 // higher = 2/lower = 0 // status = true 
> process = 1 // window.first = 5/window.second = 22 // higher = 5/lower = 22 // status = true 
process = 3 // window.first = 7/window.second = 7 // higher = 7/lower = 7 // status = false 
process = 2 // window.first = 3/window.second = 9 // higher = 9/lower = 3 // status = true 
process = 1 // window.first = 19/window.second = 14 // higher = 19/lower = 14 // status = true 
> process = 1 // window.first = 5/window.second = 11 // higher = 5/lower = 11 // status = true 
process = 1 // window.first = 35/window.second = 23 // higher = 35/lower = 23 // status = true 
process = 2 // window.first = 13/window.second = 17 // higher = 17/lower = 13 // status = true 
process = 1 // window.first = 32/window.second = 24 // higher = 32/lower = 24 // status = true 
process = 2 // window.first = 17/window.second = 18 // higher = 18/lower = 17 // status = true 
process = 1 // window.first = 22/window.second = 18 // higher = 22/lower = 18 // status = true 
> process = 1 // window.first = 8/window.second = 11 // higher = 8/lower = 11 // status = true 
process = 1 // window.first = 18/window.second = 14 // higher = 18/lower = 14 // status = true 
process = 2 // window.first = 3/window.second = 6 // higher = 6/lower = 3 // status = true 
process = 1 // window.first = 24/window.second = 16 // higher = 24/lower = 16 // status = true 
process = 1 // window.first = 9/window.second = 15 // higher = 9/lower = 15 // status = true 
process = 1 // window.first = 31/window.second = 26 // higher = 31/lower = 26 // status = true 
process = 2 // window.first = 24/window.second = 28 // higher = 28/lower = 24 // status = true 
process = 1 // window.first = 41/window.second = 30 // higher = 41/lower = 30 // status = true 
process = 2 // window.first = 13/window.second = 19 // higher = 19/lower = 13 // status = true 
+0

私には競合状態のような匂いがします...しかし、私は –

答えて

1

私はそれがあるため、変数の型であるthinik:

試してみてください。

first = parseInt($(this).attr("id")); 

私の知る限り見ることができるように
second = parseInt($(this).attr("id")); 
+0

シームがうまく動作しないとわからないので、http://www.inventpartners.com/content/javascript_is_intを使ってテストしました。 – ThomasReggi

+0

LIESの意味は何ですか? –

+0

上記のリンクで関数を使用したとき、javascriptは変数を整数として読み込んでtrueを返しましたが、そうではありませんでした。 – ThomasReggi

0

、 高を常に高い(ステータスは常に真でなければならない)でしょうか? あなたは常に(テストに応じて)高い値に高い値に影響しています。 等価(プロセス3)を除いて、statusは常にtrueです。

問題は次のようになります。出力の中で最高の数値が高くないことがあるのはなぜですか?

これは、Liangliang Zhengによって提起された点から来ています。あなたは文字列を比較しています。 文字列を比較するために、javascriptは文字単位で比較します。たとえば、最初の文字列>

"5"が "2"より高いため "5"が "22"より高くなります。

コードが数値で機能するようにするには、Liangliang Zhengの示唆するようにparseIntメソッドを使用します。

編集:そして、あなたはそれがステータスが高い(そしてあなたが高い/低い値を気にしない)によってその真または偽のもので動作するようにしたい場合は、

var status=(parseInt(window.first)>parseInt(window.second)); 

は、トリック

を行う必要があります
関連する問題