2014-02-12 73 views
1

一些JavaScript代碼基於Codecademy的JavaScript軌道上的構建「搖滾,紙張,剪刀」課程。「剪刀」按鈕的作品,但「搖滾以及 「紙」 都顯示 「錯誤!」 的消息HTML/JavaScript搖滾,紙張,剪刀遊戲無法正常工作

HTML代碼:

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript" src="script.js"></script> 
</head> 
<body> 
    <h1>Rock, Paper, Scissors</h1> 
    <button onClick='choose("rock")'>Rock</button> 
    <button onClick='choose("paper")'>Paper</button> 
    <button onClick='choose("scissors")'>Scissors</button><br> 
    <button onClick='compare(user, computerChoice)'>Go!</button> 
    <p id="result"></p> 
</body> 
</html> 

Javascript代碼:

var user; 
var choose = function(choice) { 
    user = choice; 
} 

var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} 
else if (computerChoice < 0.67) { 
    computerChoice = "paper"; 
} 
else { 
    computerChoice = "scissors"; 
} 

var compare = function(choice1, choice2) { 
    if (choice1 === choice2) { 
     document.getElementById("result").innerHTML = "Tie!"; 
    } 
    else if (choice1 === "rock") { 
     if (choice2 === "scissors") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
     else { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
    } 
    if (choice1 === "paper") { 
     if (choice2 === "rock") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
     else if (choice2 ==="scissors") { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
    } 
    if (choice1 === "scissors") { 
     if (choice2 === "rock") { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
     else if (choice2 === "paper") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
    } 
    else { 
     document.getElementById("result").innerHTML = "ERROR!"; 
    } 
} 
+2

乍一看,看起來像你錯過了其他幾個... – 2014-02-12 21:05:07

+0

@SakhalTurkaystan這就是解決方案。把它放在一個答案:-)這裏是它的小提琴:http://jsfiddle.net/5pxK6/ – Sirko

+0

謝謝。我修正了錯誤,現在它完美地工作。 – nickrodriguez13

回答

0

對於選擇1,你沒有使用 「否則,如果」 爲岩石和剪刀如果choice1不是剪刀,那麼總是會調用其他條件與錯誤。

2

乍一看,看起來你錯過,如果一對夫婦的其他...

var user; 
var choose = function(choice) { 
    user = choice; 
} 

var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} 
else if (computerChoice < 0.67) { 
    computerChoice = "paper"; 
} 
else { 
    computerChoice = "scissors"; 
} 

var compare = function(choice1, choice2) { 
    if (choice1 === choice2) { 
     document.getElementById("result").innerHTML = "Tie!"; 
    } 
    else if (choice1 === "rock") { 
     if (choice2 === "scissors") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
     else { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
    } 
    else if (choice1 === "paper") { 
     if (choice2 === "rock") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
     else if (choice2 ==="scissors") { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
    } 
    else if (choice1 === "scissors") { 
     if (choice2 === "rock") { 
      document.getElementById("result").innerHTML = "You Lose!"; 
     } 
     else if (choice2 === "paper") { 
      document.getElementById("result").innerHTML = "You Win!"; 
     } 
    } 
    else { 
     document.getElementById("result").innerHTML = "ERROR!"; 
    } 
} 
0

這裏是我的工作之一。我再次發佈它,因爲我做了一些小的改動

<HTML> 
<HEAD><TITLE></TITLE> 
<META http-equiv=Content-Type content="text/html; charset=UTF-8"> 
<STYLE type=text/CSS> 
.s{font-size:60px} 
.b{font-size:75px;background-color:##FF4000;height:85px} 
</STYLE> 
<SCRIPT type="text/javascript"> 
var p=['Rock','Scissors','Paper',['win','&#10004;'],['lose','&#9760;']] 

function g(n){ document.getElementById('g').innerHTML=p[n]; var cp=[0,1,2] 
    cp.sort(function(a,b){return Math.random()-0.5}); 
    r=Math.floor(3*(Math.random())); 
    document.getElementById('i').innerHTML=p[cp[r]];r=cp[r]; 
    if(r===n){w(1); return} 
    else if((r===0 && n===1) || (r===1 && n==2) || (r==2 && n===0)){w(2)} 
    else{w(0)} 

} 
function w(w){ 
pf=document.getElementById('gam'); pftr=pf.getElementsByTagName('tr');pftd=pf.getElementsByTagName('td'); 
    for(b=0;b<3;b++){pftd[b].innerHTML='&nbsp;'} w===1?pftd[w].innerHTML='tie':pftd[w].innerHTML='win' 
} 
</SCRIPT> 
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD> 
<BODY scroll="auto"> 
<OBJECT ID="VoiceObj" CLASSID="clsid:96749377-3391-11D2-9EE3-00C04F797396" ></OBJECT> 
<TABLE BORDER=0 style="height:200px;width:500px;text-align:center;font-size:50px" id="gam"> 
<TR> 
    <TD width="33%"> </TD><TD rowspan=2 id="ww"> </TD><TD width="33%" > </TD> 
</TR> 
<TR> 
    <TD id="g"> </TD><TD id="i"> </TD> 
</TR> 
</TABLE> 
<TABLE BORDER=1 style="border-collapse:collapse" cellpadding=0 > 
<TR> 

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp;&nbsp;Computer 


    </TR> 
<TR> 
</Table> 
<hr style="margin:30px"> 
<TABLE BORDER=1 style="border-collapse:collapse" cellpadding=0 > 

    <TD><input type="button" class="b" value="Paper" onclick="g(2)"></TD> 
    <TD><input type="button" class="b" value="Rock" onclick="g(0)"></TD> 
    <TD><input type="button" class="b" value="Scissors" onclick="g(1)"></TD> 

</TR> 
</TABLE> 



</BODY></HTML>