Added basic DOM functionality
Instead of prompt() and console now using DOM elements
This commit is contained in:
parent
fa0adb794a
commit
807ce0487b
16
index.html
16
index.html
|
@ -5,11 +5,8 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Rock Paper Scissors</title>
|
||||
<style>
|
||||
button {
|
||||
margin: 50px;
|
||||
position: absolute;
|
||||
top: calc(50% - 50px);
|
||||
left: calc(50% - 50px);
|
||||
.choose {
|
||||
margin: 20px;
|
||||
padding: 20px;
|
||||
border: 2px solid #000;
|
||||
border-radius: 35px;
|
||||
|
@ -18,13 +15,16 @@
|
|||
font-weight: 700;
|
||||
font-size: xx-large;
|
||||
cursor: pointer;
|
||||
transform: translate(-50%, -50%);
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<button onclick="game(this)">Start<br>game</button>
|
||||
<button class="choose" value="rock">Rock</button>
|
||||
<button class="choose" value="paper">Paper</button>
|
||||
<button class="choose" value="scissors">Scissors</button>
|
||||
<div id="result">
|
||||
Result:
|
||||
</div>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
51
index.js
51
index.js
|
@ -1,3 +1,5 @@
|
|||
'use strict';
|
||||
|
||||
const choices = ["rock", "paper", "scissors"];
|
||||
|
||||
function getComputerChoice() {
|
||||
|
@ -43,45 +45,12 @@ const points = (() => {
|
|||
playerAddPoints, computerAddPoints, reset};
|
||||
})()
|
||||
|
||||
function game(clickEvent) {
|
||||
clickEvent.setAttribute('onclick', ' ');
|
||||
points.reset();
|
||||
const buttons = document.querySelectorAll("button.choose");
|
||||
const resultDiv = document.querySelector("div#result");
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let correctOption = false
|
||||
let playerChoice;
|
||||
|
||||
while (correctOption == false) {
|
||||
playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?");
|
||||
if (playerChoice) {
|
||||
choices.forEach(element => {
|
||||
if (playerChoice.toLowerCase().trim() == element) {
|
||||
correctOption = true
|
||||
return;
|
||||
}
|
||||
})
|
||||
} else if (confirm(`Do you wanna quit the game?`)) {
|
||||
clickEvent.setAttribute('onclick', 'game(this)');
|
||||
throw new Error("Exited the game on user's request");
|
||||
}
|
||||
}
|
||||
|
||||
let round = playRound(playerChoice, getComputerChoice());
|
||||
let pointsString = `Points: ${points.player}:${points.computer}\n`;
|
||||
alert(pointsString + round);
|
||||
}
|
||||
|
||||
let confirmString;
|
||||
if (points.player > points.computer) {
|
||||
confirmString = `You won ${points.player}:${points.computer}`
|
||||
} else if (points.player == points.computer) {
|
||||
confirmString = `That was a ${points.player}:${points.computer} tie`
|
||||
} else if (points.player < points.computer) {
|
||||
confirmString = `You lost ${points.player}:${points.computer}`
|
||||
}
|
||||
if (confirm(`${confirmString}, do you wanna restart the game?`)) {
|
||||
game();
|
||||
}
|
||||
}
|
||||
|
||||
// game();
|
||||
buttons.forEach(button => {
|
||||
button.addEventListener("click", () => {
|
||||
let result = playRound(button.value, getComputerChoice());
|
||||
resultDiv.textContent = `Result: ${result}`;
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue