page not responding to simple JavaScript

javascript html

36 观看

1回复

1 作者的声誉

This is the html but for some reason it doesn't work. The numbers count up so that part works but it doesn't stop when winnngScore = p1Score (i.e 5)

var p1Button = document.querySelector("#p1");
var p2Button = document.getElementById("p2");
var p1Display = document.querySelector("#p1Display");
var p2Display = document.querySelector("#p2Display");
var p1Score = 0;
var p2Score = 0;
var gameOver = false;
var winningScore = 5;

p1Button.addEventListener("click", function() {
  if (!gameOver) {
    p1Score++;
    if (p1Score === winningScore) {
      gamerOver = true;
    }
    p1Display.textContent = p1Score;
  }
});

p2Button.addEventListener("click", function() {
  if (!gameOver) {
    p2Score++;
    if (p2Score === winningScore) {
      gamerOver = true;
    }
    p2Display.textContent = p2Score;
  }
})
<h1><span id="p1Display">0</span> to <span id="p2Display">0</span></h1>

<p>Playing to: 5</p>
<input type="number">
<button id="p1">Player One</button>
<button id="p2">Player Two</button>
<button id="reset">Reset</button>

作者: NimDevWorks 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

305 作者的声誉

You've misspelled gameOver as gamerOver in the if statements.

var p1Button = document.querySelector("#p1");
var p2Button = document.getElementById("p2");
var p1Display = document.querySelector("#p1Display");
var p2Display = document.querySelector("#p2Display");
var p1Score = 0;
var p2Score = 0;
var gameOver = false;
var winningScore = 5;

p1Button.addEventListener("click", function(){
    if(!gameOver){
    p1Score++;
    if(p1Score === winningScore){
        gameOver = true;
    }
    p1Display.textContent = p1Score;
}
});

p2Button.addEventListener("click", function(){
    if(!gameOver){
    p2Score++;
    if(p2Score === winningScore){
        gameOver = true;
    }
    p2Display.textContent = p2Score;
}
})
<!DOCTYPE html>
<html>
<head>
    <title>Score Keeper</title>
</head>
<body>
<h1><span id="p1Display">0</span> to <span id="p2Display">0</span></h1>

<p>Playing to: 5</p>
<input type="number">
<button id="p1">Player One</button>
<button id="p2">Player Two</button>
<button id="reset">Reset</button>

<script src="script.js"></script>
</body>
</html>

作者: vpzomtrrfrt 发布者: 2017 年 12 月 27 日
32x32