-
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
rps game #9
base: main
Are you sure you want to change the base?
rps game #9
Conversation
const bot_score = document.getElementById('bot') | ||
bot_score.innerText = bot_count | ||
|
||
const random_number_generator = ()=>(Math.ceil(Math.random()*3)) //random number generator |
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 proper spacings.
const random_number_generator = () => (Math.ceil(Math.random()*3))
update the same in the code.
if(id===1){ | ||
if(computer_choice == 2){ | ||
status_human.innerText ="Rock" | ||
stat.innerText= "You lose" | ||
status_bot.innerText= "Paper" | ||
bot_count++ | ||
bot_score.innerText = bot_count | ||
} | ||
else if(computer_choice == 3){ | ||
status_human.innerText ="Rock" | ||
stat.innerText = "You won" | ||
status_bot.innerText= "Scissor" | ||
human_count++ | ||
human_score.innerText = human_count | ||
} | ||
else{ | ||
status_human.innerText ="Rock" | ||
stat.innerText = "Score are tied" | ||
status_bot.innerText= "Rock" | ||
} | ||
|
||
stat.remove | ||
} | ||
// For paper | ||
else if(id===2){ | ||
if(computer_choice == 1) { | ||
status_human.innerText ="Paper" | ||
stat.innerText= "You won" | ||
status_bot.innerText= "Rock" | ||
human_count++ | ||
human_score.innerText = human_count | ||
} | ||
|
||
else if(computer_choice == 3){ | ||
status_human.innerText ="Paper" | ||
stat.innerText = "You Lose" | ||
status_bot.innerText= "Scissor" | ||
bot_count++ | ||
bot_score.innerText = bot_count | ||
} | ||
else{ | ||
status_human.innerText ="Paper" | ||
stat.innerText = "Score are tied" | ||
status_bot.innerText= "paper" | ||
} | ||
|
||
stat.remove | ||
} | ||
// For Scissors | ||
else{ | ||
if(computer_choice == 1) { | ||
status_human.innerText ="Scissor" | ||
stat.innerText= "You Lose" | ||
status_bot.innerText= "Rock" | ||
bot_count++ | ||
bot_score.innerText = bot_count | ||
} | ||
|
||
else if(computer_choice == 2){ | ||
status_human.innerText ="Scissor" | ||
stat.innerText = "You Won" | ||
status_bot.innerText= "Paper" | ||
human_count++ | ||
human_score.innerText = human_count | ||
} | ||
else{ | ||
status_human.innerText ="Scissor" | ||
stat.innerText = "Score are tied" | ||
status_bot.innerText= "Scissor" | ||
} | ||
|
||
stat.remove | ||
} |
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.
optimize 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 === 0 ) && (opponent === 2) ||
(you === 1 ) && (opponent === 0) ||
(you === 2) && (opponent === 1) ) // 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;
|
||
|
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.
Dont push unnecessary spaces. always check the code before updating.
Rps game initial commit