-
Notifications
You must be signed in to change notification settings - Fork 15
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
[ADD]: Rock Paper Scissor Game Code #14
base: main
Are you sure you want to change the base?
Conversation
|
||
|
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.
remove unnecessary spacings.
if (str == comp_res) { | ||
document.getElementById('result').innerHTML="Draw!"; | ||
} | ||
else if(str == 'rock' && comp_res == 'scissor') { | ||
document.getElementById('result').innerHTML='You Won'; | ||
user_score += 1; | ||
document.getElementById('user_score_display').innerHTML=user_score; | ||
} | ||
else if(str == 'paper' && comp_res == 'rock') { | ||
document.getElementById('result').innerHTML='You Won'; | ||
user_score += 1; | ||
document.getElementById('user_score_display').innerHTML=user_score; | ||
} | ||
else if(str == 'scissor' && comp_res == 'paper') { | ||
document.getElementById('result').innerHTML="You Won"; | ||
user_score += 1; | ||
document.getElementById('user_score_display').innerHTML=user_score; | ||
} | ||
else { | ||
document.getElementById('result').innerHTML='You Lose'; | ||
comp_score += 1; | ||
document.getElementById('comp_score_display').innerHTML=comp_score; | ||
} |
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.
instead of multiple if..else statemets and repeating code section use concept of ternary operator:
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;
No description provided.