Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Rock Paper Scissor Vanilla JS

Jose Gomez 100

@josegomez832

Desktop design screenshot for the Rock, Paper, Scissors game coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
4advanced
View challenge

Design comparison


SolutionDesign

Solution retrospective


I found the animation part of it or the faded 'aurora' of circles on the winning player difficult. It's all bare, no framework. I did steal the bootstrap columns and container system but everything else is straight up Vanilla JS. I do wish I can come back to rewrite the logic in a more pragmatic approach versus just being linear.

Any tips on how to reconsider this as far as JS I'm all ears. Thank you!

Community feedback

Dun 290

@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

  1. run all the code to check if player lost
  2. if not , check if its a draw
  3. if none of that , player won

efficient version

  1. check if its a draw (if so stop code immediately)
  2. 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!

0

Jose Gomez 100

@josegomez832

Posted

@DundeeA Yes makes sense. I'm not use to the whole return keyword. Thank you for this!

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord