Skip to content

Commit

Permalink
added highScore feature
Browse files Browse the repository at this point in the history
- saves the highScore in local storage to rerender it once refreshed
- used localStorage for storage
  • Loading branch information
Mano-08 committed Oct 13, 2022
1 parent a364c6c commit 7b39b78
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
29 changes: 16 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@

<body>
<div class="wrapper">
<div class="buttons">
<button class="rpsButton" value="Rock" onclick="getResult()"></button>
<button class="rpsButton" value="Paper" onclick="getResult()">🤚</button>
<button class="rpsButton" value="Scissors" onclick="getResult()"></button>
</div>
<div class="resultContainer">
<div class="buttons">
<button class="rpsButton" value="Rock" onclick="getResult()"></button>
<button class="rpsButton" value="Paper" onclick="getResult()">🤚</button>
<button class="rpsButton" value="Scissors" onclick="getResult()"></button>
</div>

<div class="resultContainer">
<div id = "scoreContainer">
<div id = "yourScore">Your Score: </div>
<div id="player-score">0</div>
<div id = "yourScore">Your Score: </div>
<div id="player-score">0</div>
</div>
<div id = "handsdiv">
<div id="hands"></div>
<div id = "handsdiv">
<div id="hands"></div>
</div>
<div id="result"></div>
<button id='endGameButton'>Again !</button>
</div>
<div id="result"></div>
<button id='endGameButton'>Again !</button>
</div>

<div id="highScore">High score: 0</div>
</div>

<script src="script.js"></script>
Expand Down
26 changes: 25 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
let highScoreFromLocalStorage = localStorage.getItem("highScore");
let highScoreEl=document.getElementById("highScore");
let highScore=0;
if (highScoreFromLocalStorage) {
highScore = highScoreFromLocalStorage;
highScoreEl.textContent="High score: " + highScore;
}

const choices = ["Rock", "Paper", "Scissors"];
function getComputerChoice(choices) {
return choices[Math.floor(Math.random() * choices.length)];
Expand Down Expand Up @@ -52,6 +60,13 @@ function showResult(scoreBefore, scoreAfter, playerChoice, computerChoice) {
}

finalresult = document.getElementById("result").innerText;
if (parseInt(scoreAfter) > highScore) {
highScore = scoreAfter;
localStorage.setItem("highScore", highScore);
highScoreFromLocalStorage = localStorage.getItem("highScore");
highScoreEl.textContent="High score: " + highScoreFromLocalStorage;
}
localStorage.setItem("highScore", highScore);
if (parseInt(scoreBefore) > parseInt(scoreAfter)) {
document.getElementById("result").style.color = "red";
document.getElementById("result").innerText = "You Lose!";
Expand All @@ -67,12 +82,14 @@ function showResult(scoreBefore, scoreAfter, playerChoice, computerChoice) {
function onClickRPS(playerChoice) {
computerChoice = getComputerChoice(choices);
getResult(playerChoice, computerChoice);

showResult(scoreBefore, scoreAfter, playerChoice, computerChoice);
}

function playGame() {
const rpsButtons = document.querySelectorAll(".rpsButton");

highScoreFromLocalStorage = localStorage.getItem("highScore");
highScoreEl.textContent="High score: " + highScoreFromLocalStorage;
rpsButtons.forEach(
(rpsButton) =>
(rpsButton.onclick = () => {
Expand All @@ -82,8 +99,15 @@ function playGame() {
}

function endGame() {
localStorage.setItem("highScore", highScore);
highScoreFromLocalStorage = localStorage.getItem("highScore");
highScoreEl.textContent="High score: " + highScoreFromLocalStorage;
const endGameButton = document.getElementById("endGameButton");
endGameButton.onclick = () => {
highScore = 0;
localStorage.setItem("highScore",highScore);
highScoreFromLocalStorage = localStorage.getItem("highScore");
highScoreEl.textContent="High score: " + highScoreFromLocalStorage;
document.getElementById("player-score").innerText = 0;
document.getElementById("result").innerText = "";
document.getElementById("hands").innerText = "";
Expand Down
1 change: 0 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ html, body {
width: 100px;
font-size: 48px;
border-radius: 30px;
background-color:white
padding: auto;
margin:auto;
color:white
Expand Down

0 comments on commit 7b39b78

Please sign in to comment.