2011-02-10 7 views
0

を失敗:JavaScriptの比較私は2つの値を比較し、その後、のプロンプトを表示しようとしている

var x,y; 
x = prompt("enter the first value",""); 
x = prompt("enter the second value",""); 

if(x > y) 
{ 
    alert("x>y"); 
} 
else if(x < y) 
{ 
    alert("y>x") 
} 
else 
{ 
    alert("error"); 
} 

を私はこれを実行するたびに、alert("error")ラインがヒットします。私は間違って何をしていますか?

+0

「x =」を2回使用しています – vol7ron

答えて

4

あなたはyを割り当てるされていません。

x=prompt("enter the first value",""); 
x=prompt("enter the second value",""); 

どちらの割り当てがxを割り当てます。

+0

ありがとうございます –

0

2行目は、xの代わりにyを設定する必要があります。

1

タイプミス:

x=prompt("enter the first value",""); 
y=prompt("enter the second value",""); 
0
x=prompt("enter the first value",""); 
x=prompt("enter the second value",""); 

は次のようになります。

x=prompt("enter the first value",""); 
y=prompt("enter the second value",""); 
0

おそらく、あなたはintentioanlly X =二度書いていませんか?

0

xを2回入力して割り当てようとしています。したがって、yundefinedです。

何かundefinedは真とはみなされません。

var x,y; 
x = prompt("enter the first value",""); 
y = prompt("enter the second value",""); 

if  (x > y) { alert("x>y"); } 
else if (x < y) { alert("y>x"); } 
else    { alert("error"); } 
関連する問題