Skip to content

Conversation

ravb-odoo
Copy link

No description provided.

Comment on lines +22 to +26
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);
Copy link
Owner

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.

Comment on lines +28 to +65
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");

}

}
Copy link
Owner

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants