-
Notifications
You must be signed in to change notification settings - Fork 15
[ADD] Rock-paper-scissor game #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
var a = this.computer_choice(); | ||
let b = this.choice; | ||
let c = Number(document.getElementById('computer_score').textContent); | ||
let u = Number(document.getElementById('user_score').textContent); | ||
let n = Number(document.getElementById('points').value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use better naming conventions, representing the property of value assingned.
if (n) { | ||
document.getElementById('computer_img').src = `img/${a}.png` | ||
document.getElementById('user_img').src = `img/${b}.png` | ||
if ((a == "rock" & b == "scissor") | (a == "scissor" & b == "paper") | (a == "paper" & b == "rock")) { | ||
document.getElementById('computer_score').innerHTML = c += 1; | ||
document.getElementById('score').innerHTML = "Computer Wins"; | ||
} | ||
else if ((b == "rock" & a == "scissor") | (b == "scissor" & a == "paper") | (b == "paper" & a == "rock")) { | ||
document.getElementById('user_score').innerHTML = u += 1; | ||
document.getElementById('score').innerHTML = "User Wins"; | ||
} else { | ||
document.getElementById('score').innerHTML = "Tie"; | ||
} | ||
} | ||
|
||
if (n == c) { | ||
const buttons = document.getElementsByClassName("btn"); | ||
for (let i = 0; i < buttons.length; i++) { | ||
buttons[i].disabled = true; | ||
} | ||
document.getElementById("computer_header").classList.add("bg-success"); | ||
document.getElementById("computer_body").classList.add("border-success"); | ||
document.getElementById("user_header").classList.add("bg-danger"); | ||
document.getElementById("user_body").classList.add("border-danger"); | ||
} | ||
else if (n == u) { | ||
const buttons = document.getElementsByClassName("btn"); | ||
for (let i = 0; i < buttons.length; i++) { | ||
buttons[i].disabled = true; | ||
} | ||
document.getElementById("user_header").classList.add("bg-success"); | ||
document.getElementById("user_body").classList.add("border-success"); | ||
document.getElementById("computer_header").classList.add("bg-danger"); | ||
document.getElementById("computer_body").classList.add("border-danger"); | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update this code using ternary operator, avoid using multiple if..else and repeating statements,
what i can see is there are only three cases when user can win:
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 for computer;
No description provided.