Skip to content

Implementing a rps game which is based on javascript #13

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gusi-odoo
Copy link

@gusi-odoo gusi-odoo commented Feb 7, 2023

Rock Paper Scissor game in js

let losses = 0;
let ties = 0;

function ABC(value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use a better naming convention that represent the property of you code:
instead of ABC , use gameState/ updateStatus ... etc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry sir next time i will take care of this

Comment on lines 10 to 26
if (value == 'rock' && rand == "scissor" ||
value == "paper" && rand == "rock" ||
value == "scissor" && rand == "paper") {
document.getElementById("win").innerHTML = wins += 1;
document.getElementById("print_result").innerHTML += `You Win`;

} else if (rand == 'rock' && value == "scissor" ||
rand == "paper" && value == "rock" ||
rand == "scissor" && value == "paper") {
document.getElementById("lose").innerHTML = losses += 1;
document.getElementById("print_result").innerHTML += `Computer Win`;

} else {
document.getElementById("tie").innerHTML = ties += 1;
document.getElementById("print_result").innerHTML += `it's a Tie`;

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using repetitive piece of code, store it in one variable and use it, here
update the code using ternary operator instead of if..else
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 fro 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