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.
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.
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.
Dont push unnecessary spaces. always check the code before updating.
Rps game initial commit