简体   繁体   English

无法让我的函数多次运行并破坏我的代码

[英]Can't get my function to run more than once and break my code

I'm trying to run this function until the player or the computer wins 5 times. 我正在尝试运行此功能,直到播放器或计算机获胜5次。 Without the while loop, it runs one time and everything works fine. 没有while循环,它运行一次,一切正常。 As soon as I add a while loop, the function still runs only once and it gives me an undefined return. 只要我添加一个while循环,该函数仍然只运行一次,它给我一个未定义的返回。

 function playToFive() { console.log('Let\\'s play Rock Paper Scissors'); var playerWins = 0; var computerWins = 0; while (playerWins === 5 || computerWins === 5) { if (humanVsMachine === 'player') { playerWins += 1; } else if (humanVsMachine === 'computer') { computerWins += 1; } return [playerWins, computerWins]; } } console.log(playToFive()); 

while(playerWins === 5 || computerWins === 5)

Your while loop will actually never execute, since you're checking for equality and both playerWins and computerWins are 0 initially. 你的while循环实际上永远不会执行,因为你正在检查是否相等,并且playerWinscomputerWins最初都是0

You may be looking for a condition more like this: 您可能正在寻找更像这样的条件:

while(playerWins < 5 && computerWins < 5)

Note that we're using the logical AND && instead of the logical OR || 请注意,我们使用逻辑AND &&而不是逻辑OR || . This is because you don't want to keep looping until both of them win. 这是因为你不想在他们两个都获胜之前保持循环。 The logical OR means that even if the computer has won but the player hasn't, we'll continue looping. 逻辑OR意味着即使计算机已赢但玩家没有,我们将继续循环。 Only one of the conditions needs to be true for the whole statement to be true. 只有其中一个条件需要成立才能使整个陈述成为现实。

When we use the logical AND, if one of them is false (meaning, if only one player has reached 5 wins already), then we will break out of the loop as we should. 当我们使用逻辑AND时,如果其中一个是假的(意思是,如果只有一个玩家已经达到5胜),那么我们将按照我们应该的方式突破循环。

The next problem is that you have a return statement in your while loop, so after the first execution, even if 5 wins haven't been reached yet, it will return the array of the player's wins and the computer's wins. 接下来的问题是你的while循环中有一个return语句,所以在第一次执行之后,即使还没有达到5次胜利,它也会返回玩家获胜的数组和计算机的胜利。

You should put the return statement after the loop so that you run the loop 5 times and then return after somebody has won. 你应该在循环之后放置return语句这样你就可以运行循环5次,然后在有人赢了之后返回。

And finally, since you haven't provided the rest of the code, I'm not sure if humanVsMachine is actually defined; 最后,既然你没有提供剩下的代码,我不确定是否实际定义了humanVsMachine ; if you defined that outside of the function then you're good to go. 如果你在函数之外定义了那么你就好了。

After the if-else structure is evaluated and executed, a return statement is called unconditionally, thus terminating the loop prematurely. 在评估并执行if-else结构之后,无条件地调用return语句,从而过早地终止循环。 You should place the return after the loop instead. 您应该在循环之后放置return Additionally, the loop condition should be as long as neither player reaches 5 wines (evaluated with the < operator): 此外,循环条件应该是任何玩家都不会达到5个葡萄酒(用<运算符评估):

while (playerWins < 5 && computerWins < 5 ) {
    if (humanVsMachine === 'player') {
        playerWins +=1;
    } else if (humanVsMachine === 'computer') {
        computerWins += 1;
    }
}
return [playerWins, computerWins];

你需要在while循环之外移动return语句,但是你还需要在while循环中改变你的条件 - 现在只有当playerWinscomputerWins正好是5时它才会运行,而实际上它需要停止在那一点上运行(所以while(playerWins < 5 && computerWins < 5)

Return will terminate your loop 返回将终止你的循环

function returnMe() {
  for (var i=0; i<2; i++) {
    if (i === 1) return i;
  }
}

alert(returnMe()); Try this out 


function playToFive() {
    console.log('Let\'s play Rock Paper Scissors');
    var playerWins = 0;
    var computerWins = 0;
    while (playerWins === 5 || computerWins === 5) {
        if (humanVsMachine === 'player') {
        playerWins += 1;
        } else if (humanVsMachine === 'computer') {
        computerWins += 1;
        }
    // return [playerWins, computerWins]; moving code to below
    }
    return [playerWins, computerWins]; // moved return outside the loop here
}

console.log(playToFive());

You're returning inside the while loop, which will end the looping process. 你将在while循环中返回,这将结束循环过程。 Therefore move your return code outside the loop like below. 因此,请将返回代码移到循环外部,如下所示。 Also notice that your logical statement is not going to allow the loop to run properly. 另请注意,您的逻辑语句不允许循环正常运行。

function playToFive() {
    console.log('Let\'s play Rock Paper Scissors');
    var playerWins = 0;
    var computerWins = 0;
    while (playerWins < 5 && computerWins < 5 ) {
        if (humanVsMachine === 'player') {
        playerWins += 1;
        } else if (humanVsMachine === 'computer') {
        computerWins += 1;
        }
    // return [playerWins, computerWins]; moving code to below
    }
    return [playerWins, computerWins]; // moved return outside the loop here
}

console.log(playToFive());

Allow me to add that spacing your code helps a lot, it's a little bit 'vertical' looking with the closing brackets. 请允许我添加代码间距帮助很多,使用右括号看起来有点'垂直'。 Note how spacing on the above helps to visually identify things like this :) 请注意上面的间距有助于在视觉上识别这样的事情:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM