Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 事件侦听器中的if-else语句只有一部分工作_Javascript_Html_Dom Events - Fatal编程技术网

Javascript 事件侦听器中的if-else语句只有一部分工作

Javascript 事件侦听器中的if-else语句只有一部分工作,javascript,html,dom-events,Javascript,Html,Dom Events,这里是代码新手。所以我想对我的石头剪刀游戏做些改变。我希望分数被捕获,以及显示在页面上的结果文本。每次单击按钮时都会显示文本,但一旦我添加了+1增量和代码以在屏幕上实时显示分数,文本就不会显示。我只是先在“rock”事件侦听器上测试一下,以确保它能正常工作 //Selects the classes .rock .paper .scissors const rock = document.querySelector(".rock") const paper = documen

这里是代码新手。所以我想对我的石头剪刀游戏做些改变。我希望分数被捕获,以及显示在页面上的结果文本。每次单击按钮时都会显示文本,但一旦我添加了+1增量和代码以在屏幕上实时显示分数,文本就不会显示。我只是先在“rock”事件侦听器上测试一下,以确保它能正常工作

//Selects the classes .rock .paper .scissors
const rock = document.querySelector(".rock")
const paper = document.querySelector(".paper")
const scissors = document.querySelector(".scissors")

//new div under results
const results = document.getElementById("results");

//new items
let itemRock = document.createElement('h3');
let itemPaper = document.createElement('h3');
let itemScissors = document.createElement('h3');
    
//variable for computerPlay() function to be printed to #results
itemRock.textContent = 'You have chosen rock!';
itemPaper.textContent = 'You have chosen paper!';
itemScissors.textContent = 'You have chosen scissors!';

//Player + computer score for count purposes
let playerScore = 0;
let computerScore = 0;

rock.addEventListener('click', function() {
    computerPlay();
    if(computerPlay() === 'paper') {
        itemRock.textContent = 'You have chosen rock and the computer has chosen paper.' + 'You lose, paper beats rock! Try again';
        computerScore += 1;
         document.getElementById("computer-score").innerText = computerScore;
    } else if (computerPlay() === 'rock') {
        itemRock.textContent = 'You have chosen rock and the computer has chosen rock.' +  'Its a draw, try again';
    } else {
        itemRock.textContent = 'You have chosen rock and the computer has chosen 
        scissors. ' + 'You win! Rock beats scissors.';
        playerScore += 1;
        document.getElementById("player-score").innerText = playerScore;
    }
    return 'rock';  
});
HTML代码:

<div class="choice">

<button type="button" class="rock">Rock</button>
<button type="button" class="paper">Paper</button>
<button type="button" class="scissors">Scissors</button>

</div>
    <div id="results">
</div>
<p>Player Score: <a id="player-score">0</a></p>
<p>Computer Score: <a id="computer-score">0</a></p>

摇滚乐
纸张
剪刀
球员得分:0

电脑成绩:0


任何和所有的帮助将不胜感激

您应该调用
computerPlay
功能一次

rock.addEventListener('click', function() {
    
    const result = computerPlay();
    
    if (result) {
         itemRock.textContent = 'You have chosen rock and the computer has chosen paper. 
         ' + 'You lose, paper beats rock! Try again';
         computerScore += 1;
         document.getElementById("computer-score").innerText = computerScore;
    } else if(result) {
         itemRock.textContent = 'You have chosen rock and the computer has chosen rock. 
         ' +  'Its a draw, try again';
    } else {
         itemRock.textContent = 'You have chosen rock and the computer has chosen 
         scissors. ' + 'You win! Rock beats scissors.';
         playerScore += 1;
         document.getElementById("player-score").innerText = playerScore;
    }
  
    return 'rock';  
})
像这样调用
computerPlay
函数

computerPlay();
if (computerPlay() === 'paper')
let computersHand = computerPlay();

没有意义,因为您调用它两次(计算机在一次单击中播放两次)

您应该调用
计算机播放功能一次

rock.addEventListener('click', function() {
    
    const result = computerPlay();
    
    if (result) {
         itemRock.textContent = 'You have chosen rock and the computer has chosen paper. 
         ' + 'You lose, paper beats rock! Try again';
         computerScore += 1;
         document.getElementById("computer-score").innerText = computerScore;
    } else if(result) {
         itemRock.textContent = 'You have chosen rock and the computer has chosen rock. 
         ' +  'Its a draw, try again';
    } else {
         itemRock.textContent = 'You have chosen rock and the computer has chosen 
         scissors. ' + 'You win! Rock beats scissors.';
         playerScore += 1;
         document.getElementById("player-score").innerText = playerScore;
    }
  
    return 'rock';  
})
像这样调用
computerPlay
函数

computerPlay();
if (computerPlay() === 'paper')
let computersHand = computerPlay();

没有意义,因为您调用它两次(计算机在一次单击中播放两次)

我看到您每次都在通过if/else语句调用computerPlay()函数。假设computerPlay()随机返回石头、布或剪刀,则返回不同的值。您可以做的一件简单的事情是将返回的值存储在这样的变量中

computerPlay();
if (computerPlay() === 'paper')
let computersHand = computerPlay();
然后在执行if/else语句时使用此变量

if (computersHand === 'rock') {
    // ...
} else if (computersHand === 'paper') {
    // ...
} else {
    // ...
}
如果出于任何原因不想将返回值存储到变量中,可以使用switch语句

switch (computerPlay()) {
   case 'rock':
       // ...
       break;
   case 'paper':
       // ...
       break;
   case 'scissors':
       // ...
       break;
}

我建议您阅读

我看到您每次都在通过if/else语句调用computerPlay()函数。假设computerPlay()随机返回石头、布或剪刀,则返回不同的值。您可以做的一件简单的事情是将返回的值存储在这样的变量中

computerPlay();
if (computerPlay() === 'paper')
let computersHand = computerPlay();
然后在执行if/else语句时使用此变量

if (computersHand === 'rock') {
    // ...
} else if (computersHand === 'paper') {
    // ...
} else {
    // ...
}
如果出于任何原因不想将返回值存储到变量中,可以使用switch语句

switch (computerPlay()) {
   case 'rock':
       // ...
       break;
   case 'paper':
       // ...
       break;
   case 'scissors':
       // ...
       break;
}

我建议你读一读

哦,我明白你的意思了。我不认为“如果”声明的开头会称之为电脑游戏。其实不知道。谢谢你的帮助,我明白你的意思了。我不认为“如果”声明的开头会称之为电脑游戏。其实不知道。对于帮助,它清除了很多错误,上面的海报也说了同样的事情,我不止一次调用我的函数。我不知道if-else语句是这样的。我不知道如果别人真的叫它哈哈。我试着更频繁地使用“开关”,只是为了让它更具可读性。谢谢你的帮助是的,上面的海报说了同样的事情,我不止一次地调用我的函数。我不知道if-else语句是这样的。我不知道如果别人真的叫它哈哈。我试着更频繁地使用“开关”,只是为了让它更具可读性。谢谢你的帮助