为什么我的“ Rock Paper Scissors”程序无法正常工作并使我的页面崩溃?

时间:2019-10-27 01:40:44

标签: javascript html function crash

我的“岩纸剪刀”作业也要求我也使用非常特定的直径。它根本没有工作。该页面甚至不会加载,而不显示任何已记录的控制台。

我最初在控制台中拥有所有字符串,这些警报会在页面上弹出,但是没有用,所以我只是将其切换到控制台,希望如果我使用节点时会弹出它,但这也没有不行我在播放器上移动了一下,然后通过计算机上的功能获胜,以查看是否可以更改任何内容,但这并没有改变。


var hands = ['Rock', 'Paper', 'Scissors'];

function getHand(){
        return hands[parseInt(Math.random()*hands.length)%3];
}

var player1 = "Mike";
console.log("Hello " + player1);

var player2;
console.log("Player 2 is the computer!");

//var playWins = 0;
//var compWins = 0;

function playRound(){

    if (hand == computer){
        console.log("You guys tied!");

    } else if (hand == "Scissors" && computer == "Rock"){
        console.log(player1 + " has lost! Sadddd...");
        compWins++;
    } else if (hand == "Paper" && computer == "Rock"){
        console.log(player1 + " has wonnnn!!! Yay! I guess...");
        playWins++

    } else if (hand == "Rock" && computer == "Scissors"){
        console.log(player1 + " has wonnnn!!! Yay! I guess...");
        playWins++;
    } else if (hand == "Paper" && computer == "Scissors"){
        console.log(player1 + + " has lost! Sadddd...");
        compWins++;

    } else if (hand == "Rock" && computer == "Paper"){
        console.log(player1 + + " has lost! Sadddd...");
        compWins++;
    } else if (hand == "Paper" && computer == "Paper"){
        console.log(player1 + " has wonnnn!!! Yay! I guess...");
        playWins++;
    } else {console.log("There seems to be a problem.");}
}


hand = [];
computer = [];


while (hand <= 3 || computer <= 3){
    for (i = 0; i >=5; i++ ){
    hand.push(getHand());
    computer.push(getHand());

    playRound();
    }
}

if(playWins>compWins){
    console.log(player1 + " Wins!");
} else {
    console.log("Computer Wins!");
}

我希望它能告诉我谁赢得了每场比赛,然后谁又赢得了至少5场比赛。我看不到错误消息,因为页面甚至无法完全加载。

1 个答案:

答案 0 :(得分:0)

您有一个无限循环。发生了一些错误。首先,这些是数组:

while (hand <= 3 || computer <= 3)

所以我猜你想检查他们的 length 吗?:

while (hand.length <= 3 || computer.length <= 3)

然后您在这里也有逻辑错误:

for (i = 0; i >=5; i++ ){

由于i0开头,所以它不是 >= 5,因此永远不会进入for循环,而{{1 }}循环无限期地重复。您可能需要while

<=

您还希望取消注释这些变量,以便定义变量,因为稍后将使用这些变量:

for (i = 0; i <= 5; i++ ){