I am new to programming. I'm trying to code some kind of scorekeeper in JS.
How can I compare two variables: pointOne
with maxScore
?
I want to have something like this:
If pointOne === maxScore -> Player1
wins and game is over.
Currently, this part of the code cannot compare this variable.
This is my code:
const maxInput = document.querySelector('#number');
maxInput.addEventListener('input', function() {
let maxScore = number.value;
maxScore = parseInt(this.value);
console.log(`Max score is ${maxScore}`);
// if (pointOne === 3){
// console.log("mamy 3!");
// }
})
function headerUpdate() {
const header = document.querySelector('h1');
header.innerText = `${pointOne} to ${pointTwo}`;
}
let pointOne = 0;
let pointTwo = 0;
function addPoints() {
const buttonClickTrue = document.getElementById('btn_one');
const buttonClickTrue2 = document.getElementById('btn_two');
const resetButtonTrue = document.getElementById('btn_reset');
buttonClickTrue.addEventListener('click', function() {
pointOne++;
console.log(`Your score is ${pointOne}`);
headerUpdate();
})
buttonClickTrue2.addEventListener('click', function() {
pointTwo++;
console.log(`Second score is ${pointTwo}`);
headerUpdate();
})
resetButtonTrue.addEventListener('click', function() {
window.location.reload(true);
})
}
addPoints();
* {
box-sizing: border-box;
margin: 2px;
padding: 0;
background-color: #7c86ac;
}
.game-box {
height: 30%;
display: flex;
align-items: center;
flex-direction: column;
}
#tenis {
width: 30%;
margin-top: 30px;
}
#btn_one {
background-color: green;
border: 3px solid black;
width: 30%;
}
#btn_two {
background-color: orangered;
border: 3px solid black;
width: 30%;
}
#btn_reset {
background-color: red;
border: 3px solid black;
width: 30%;
}
#number {
background-color: white;
border: 3px solid black;
width: 5%;
}
<div class="game-box">
<img src="./img/tenis_image.jpg" alt="tenis" id="tenis">
<h2>Score Keeper</h2>
<h1>0 to 0</h1>
<h3>Use button bellow to count</h3>
<p id="max">Playing to</p>
<input type="number" name="number" id="number">
<button id="btn_one">+1 Player One</button>
<button id="btn_two">+1 Player Two</button>
<button id="btn_reset">Reset</button>
</div>