@DundeeA
Posted
Hey great job completing this challenge, I took a look at the javascript and I have some suggestions.
Currently in your game logic you have a series of "and or" statements to decide if the player and the computer had the same selection. The same functionality can be achieved with a Strict equality operator.
if(selection === computerSelection) {document.getElementById('message').innerHTML = 'Draw';}
Secondly , You're making the computer run extra code it doesn't have to run to fix this you can re-order your game logic.
What your code is doing
- run all the code to check if player lost
- if not , check if its a draw
- if none of that , player won
efficient version
- check if its a draw (if so stop code immediately)
- check if player lost (if not player won)
Here's how I would restructure your code.
if(selection === computerSelection) {
document.getElementById('message').innerHTML = 'Draw';
return; // will stop the game logic immediately so no extra code is ran.
}
if(selection == 'Paper' && computerSelection == 'Scissors' || selection ==
"Scissors" &&
computerSelection == 'Rock' || selection == 'Rock' && computerSelection ==
'Paper'){
wins--;
document.getElementById('message').innerHTML = 'You Lose';
document.querySelector('#Computerselected
.userSelection').classList.add('win');
document.getElementById('wins').innerHTML = wins;
return; // player lost so stop the game logic
}
//if the code didn't hit any return statements the player won
wins++;
document.getElementById('message').innerHTML = 'You Win';
document.querySelector('#selected .userSelection').classList.add('win');
document.getElementById('wins').innerHTML = wins;
Hopefully this makes sense if you have any questions let me know!
@josegomez832
Posted
@DundeeA Yes makes sense. I'm not use to the whole return keyword. Thank you for this!