From 747fcb1444dc0a712a6ac6c409b56b9c360d64e0 Mon Sep 17 00:00:00 2001 From: Shpetim Aliu <106196770+shpetimaliu@users.noreply.github.com> Date: Sat, 22 Apr 2023 01:03:33 +0200 Subject: [PATCH] main.js Some main changes: 1. Use of const instead of var for variables that don't change value. 2. Use of template literals (${variable}) instead of concatenating strings with +. 3. Use of shorter and clearer variable names. 4. Changing the name of the function from "winer()" to "displayResult()" to be more clear about what the function does. 5. Adding an event listener for the refresh button instead of a separate function. --- Dicee Game/main.js | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Dicee Game/main.js b/Dicee Game/main.js index 9ad01ab..12cc5dd 100644 --- a/Dicee Game/main.js +++ b/Dicee Game/main.js @@ -1,29 +1,27 @@ -var rendom1 = Math.floor(Math.random() * 6)+1; -var rendom2 = Math.floor(Math.random() * 6)+1; -var rendomImg1 = "images/dice"+rendom1+".png" -var rendomImg2 = "images/dice"+rendom2+".png" +// Declaring variables and creating random images +const random1 = Math.floor(Math.random() * 6) + 1; +const random2 = Math.floor(Math.random() * 6) + 1; +const randomImg1 = `images/dice${random1}.png`; +const randomImg2 = `images/dice${random2}.png`; -function winer(){ - image(); -if(rendom1 < rendom2){ - document.querySelector("h1").innerHTML="Player 2 wins"; - }else if(rendom1 === rendom2){ - document.querySelector("h1").innerHTML="Drow" - }else { - document.querySelector("h1").innerHTML="Player 1 wins" - } -} +// Displaying images and identifying the winner +function displayResult() { + document.querySelector(".img1").setAttribute("src", randomImg1); + document.querySelector(".img2").setAttribute("src", randomImg2); -function image(){ - document.querySelector(".img1").setAttribute("src", rendomImg1); - document.querySelector(".img2").setAttribute("src", rendomImg2); + if (random1 < random2) { + document.querySelector("h1").innerHTML = "Player 2 wins"; + } else if (random1 === random2) { + document.querySelector("h1").innerHTML = "Draw"; + } else { + document.querySelector("h1").innerHTML = "Player 1 wins"; + } } - winer(); - - function refreshPage(){ - window.location.reload(); - -} - +// Calling the function to display the result and identify the winner +displayResult(); +// Page refresh on click of the refresh button +document.querySelector("button").addEventListener("click", () => { + location.reload(); +});