var gameFunction = function()
{
var userChoice = prompt("What do you choose: rock, paper, or scissors?")
var computerChoice = Math.random();
if (0 < computerChoice < 0.33)
{
computerChoice = "rock";
}
else if (0.33 < computerChoice < 0.67)
{
computerChoice = "scissors";
}
else
{
computerChoice = "paper";
}
console.log("Computer choice: ",computerChoice)
if (userChoice === computerChoice)
{
return "The result is a tie! Enter a new result?"
gameFunction();
}
else if (userChoice === "rock")
{
if (computerChoice === "scissors")
{
return "rock wins"
}
else if (computerChoice === "paper")
{
return "paper wins"
}
}
else if (userChoice === "paper")
{
if (computerChoice === "rock")
{
return "paper wins"
}
else if (computerChoice === "scissors")
{
return "scissors win"
}
}
else if (userChoice === "scissors")
{
if (computerChoice === "paper")
{
return "scissors wins"
}
else if (computerChoice === "rock")
{
return "rock win"
}
}
}
gameFunction();
這是來自Codecademy:Javascript的「Rock paper scissors」遊戲的9/9部分。當被調用時Javascript函數無法運行
我的問題是這樣的:
當用戶和計算機的關係,它應該重新運行整個「gameFunction」功能,這意味着它應該從用戶提出一個新的輸入,並從一開始一個新的輸入電腦。
但是,該程序只是打印出「結果是一條領帶!」無需重新運行「gameFunction」。我怎樣才能解決這個問題?
'如果(0'如果(0
Tushar
後'return',沒有更多的代碼在函數內執行 – Reeno