Skip to content

Commit

Permalink
Refactored Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques8787 committed Sep 12, 2024
1 parent 92c86d1 commit 0516f4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 12 additions & 19 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;

const displayMessage = function (message) {
document.querySelector(".message").textContent = message;
};

document.querySelector(".again").addEventListener("click", function () {
secretNumber = Math.trunc(Math.random() * 20) + 1;
score = 20;

document.querySelector(".message").textContent = "Start guessing...";
displayMessage("Start guessing...");
document.querySelector(".number").textContent = "?";
document.querySelector(".score").textContent = score;
document.querySelector(".guess").value = "";
Expand All @@ -20,11 +23,12 @@ document.querySelector(".check").addEventListener("click", function () {

//When there is no input
if (!guess) {
document.querySelector(".message").textContent = "No number!";
// document.querySelector(".message").textContent = "No number!";
displayMessage("No number!");

//When player wins
} else if (guess === secretNumber) {
document.querySelector(".message").textContent = "πŸŽ‰ Correct Number!";
displayMessage("πŸŽ‰ Correct Number!");
document.querySelector(".number").textContent = secretNumber;

document.querySelector("body").style.backgroundColor = "#60b347";
Expand All @@ -36,25 +40,14 @@ document.querySelector(".check").addEventListener("click", function () {
document.querySelector(".highscore").textContent = highscore;
}

//When guess is too high
} else if (guess > secretNumber) {
if (score > 1) {
document.querySelector(".message").textContent = "πŸ“‰ Too high!";
score--;
document.querySelector(".score").textContent = score;
} else {
document.querySelector(".message").textContent = "πŸ’₯ you lost the game!";
document.querySelector(".score").textContent = 0;
}

//When guess is too low
} else if (guess < secretNumber) {
//When guess is wrong
} else if (guess !== secretNumber) {
if (score > 1) {
document.querySelector(".message").textContent = "πŸ“‰ Too low!";
displayMessage(guess > secretNumber ? "πŸ“ˆ Too high!" : "πŸ“‰ Too Low!");
score--;
document.querySelector(".score").textContent = score;
} else {
document.querySelector(".message").textContent = "πŸ’₯ you lost the game!";
displayMessage("πŸ’₯ You lost the game!");
document.querySelector(".score").textContent = 0;
}
}
Expand Down

0 comments on commit 0516f4a

Please sign in to comment.