Rock Paper Scissor by deso Initial Commit#4
Conversation
| const random = () => { | ||
| return Math.floor(Math.random() * 3); | ||
| }; |
There was a problem hiding this comment.
keep the declaration before usage, for better understanding.
| // const object = { | ||
| // p1: 1, | ||
| // p2: 2, | ||
| // p3: 3, | ||
| // p4: { | ||
| // p5: 4, | ||
| // }, | ||
| // }; | ||
| // console.log(document.getElementsByTagName("p")); | ||
|
|
||
| // const prom = new Promise((res, rej) => { | ||
| // setTimeout(() => { | ||
| // rej("Working"); | ||
| // }, 10000); | ||
| // }); | ||
|
|
||
| // console.log( | ||
| // prom.then(() => console.log("done")).catch(() => console.log("rejected")) | ||
| // ); | ||
|
|
||
| fetch("https://portal.tycoonstats.com/api/demo") | ||
| .then((response) => response.text()) | ||
| .then((data) => console.log(data)); | ||
|
|
||
| async function a() { | ||
| let response = await fetch("https://portal.tycoonstats.com/api/demo"); | ||
| let data = await response.text(); | ||
| console.log(data); | ||
| } | ||
|
|
||
| a(); |
There was a problem hiding this comment.
- Make separate commit for different work.
- Double check your code before force pushing.
- Donot push unused / commented out code , make habit of checking the code with "git diff" before updating the commit.
| const play = () => { | ||
| rockBtn.addEventListener("click", () => { | ||
| let computerChoice = random(); | ||
| computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
| humanChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
| humanChoiceSpan.innerText = "Rock"; | ||
| if (computerChoice === 0) { | ||
| result.innerText = "Draw"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
| } else if (computerChoice === 1) { | ||
| result.innerText = "Computer Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand fa-5x"></i>'; | ||
| computerScore.innerText++; | ||
| } else { | ||
| result.innerText = "You Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
| humanScore.innerText++; | ||
| } | ||
| }); | ||
| paperBtn.addEventListener("click", () => { | ||
| let computerChoice = random(); | ||
| computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
| humanChoiceIcon.innerHTML = '<i class="fa-solid fa-hand fa-5x"></i>'; | ||
| humanChoiceSpan.innerText = "Paper"; | ||
| if (computerChoice === 0) { | ||
| result.innerText = "You Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
| humanScore.innerText++; | ||
| } else if (computerChoice === 1) { | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand fa-5x"></i>'; | ||
| result.innerText = "Draw"; | ||
| } else { | ||
| result.innerText = "Computer Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
| computerScore.innerText++; | ||
| } | ||
| }); | ||
| scissorBtn.addEventListener("click", () => { | ||
| let computerChoice = random(); | ||
| computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
| humanChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
| humanChoiceSpan.innerText = "Scissor"; | ||
| if (computerChoice === 0) { | ||
| result.innerText = "Computer Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
|
|
||
| computerScore.innerText++; | ||
| } else if (computerChoice === 1) { | ||
| result.innerText = "You Won!!!"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand fa-5x"></i>'; | ||
| humanScore.innerText++; | ||
| } else { | ||
| result.innerText = "Draw"; | ||
| computerChoiceIcon.innerHTML = | ||
| '<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
| } | ||
| }); |
There was a problem hiding this comment.
for this specific case we can add logic of querySelectorAll("botton"), and for each button click we can call a method that will update the scenario who win.
Re- write the code: using concept of:
querySelectorAll("botton"), use foreach, and onclick call one method that will update the state, also avoid using multiple if...else and repeating code, use ternary operator as user can win in three cases only
you_win = ( (you === "rock") && (opponent === "scissor") ||
(you === "paper") && (opponent === "rock") ||
(you === "scissor") && (opponent === "rock") ) // returns true for your win cases only.
so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;
No description provided.