Design comparison
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
- @DundeeAPosted about 2 years ago
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!
0@josegomez832Posted about 2 years ago@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 GitHubJoin 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