2016-06-30 39 views
0
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 '如果(0 Tushar

+1

後'return',沒有更多的代碼在函數內執行 – Reeno

回答

1

return語句執行後沒有行..嘗試

gameFunction(); 
return "The result is a tie! Enter a new result?" 
+0

但是最終會在第二個遊戲結束後顯示原始綁定遊戲*的結果,難道不會讓玩家感到困惑嗎? – nnnnnn

+0

u可以通過setTimeout函數給出延遲。邏輯和要求 –

0

改變返回警告(),如下圖所示:

來源:

return "The result is a tie! Enter a new result?" 

要:

alert("The result is a tie! Enter a new result?"); 
1

返回狀態ement退出「gameFunction」函數,因此它不執行下一行。嘗試使用提示,而不是像這樣:

if (userChoice === computerChoice) 
{ 
    prompt("The result is a tie! Enter a new result?"); 
    gameFunction(); 
} 

通過這種方式,用戶可以對您的響應提示,你可以用它來決定,如果比賽繼續。你總是可以只使用一個警報以及:)

0

你看這個:https://jsfiddle.net/41gcfL6g/

這裏的事情是增加一個參數的功能,這樣你就可以判斷是否已經打領帶你玩過最後一次。然後在平局的情況下,而不是調用返回後的功能,您返回gameFunction(內gameFunction

1

的recusive gameFunction()方法的結果),因爲 控制返回到第一通話功能。

if (userChoice === computerChoice) 
{ 
    return "The result is a tie! Enter a new result?" 
    gameFunction(); 
} 

因此,不是返回,您可以在那裏打印一條消息,顯示有一條平行線。

if (userChoice === computerChoice) 
{ 
    alert("The result is a tie! Enter a new result?") 
    gameFunction(); 
} 

當上述條件不滿足時,它只是返回到呼叫區域並停止。

0

看到小提琴,不要使用收益,CONSOLE.LOG https://jsfiddle.net/o62vda05/

var userChoice; 

function startGame() 
{ 
    userChoice = prompt("What do you choose: rock, paper, or scissors?"); 
    gameFunction(); 
} 

function gameFunction() 
{ 
    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) { 
     console.log("The result is a tie! Enter a new result?"); 
     startGame(); 
    } else if (userChoice === "rock") { 
     if (computerChoice === "scissors") 
     { 
      console.log("rock wins"); 
     } 
     else if (computerChoice === "paper") 
     { 
      console.log("paper wins"); 
     } 
    } else if (userChoice === "paper") { 
     if (computerChoice === "rock") { 
      console.log("paper wins"); 
     } else if (computerChoice === "scissors") { 
      console.log ("scissors win"); 
     } 
    } else if (userChoice === "scissors") { 
     if (computerChoice === "paper") { 
      console.log ("scissors wins"); 
     } else if (computerChoice === "rock") { 
      console.log("rock win"); 
     } 
    } 
} 

startGame();