DEV Community

Discussion on: Need help with The Odin's Project, Rock Paper Scissors assignment ️

Collapse
 
badgamerbad profile image
Vipul Dessai • Edited

Well done, you have completed it, but i think instead of using if else and repeated code, you should use a hash map
To do so you have to use "Map" datatype available in JS, then do the mapping for what beats what

paper => rock
scissor => paper
rock => scissor
Enter fullscreen mode Exit fullscreen mode

Then you can get the mapped value by using player weapon as a key for the hash map and if that value is equal to the computer selection, then player has beat the computer else its a win for the computer

if(ruleMap.get(playerSelection) === computerSelection) {
    playerScoreCount++;
}
else {
    computerScoreCount++;
}
Enter fullscreen mode Exit fullscreen mode

Edge case will be to check first if both have selected a same weapon and then check the mapping.

Collapse
 
taepal467 profile image
Chantae P.

Thank you for showing me a different perspective on writing code. But hash mapping is way too advanced for me right now. But I will definitely keep this as a future reference for when I get more advanced with using JavaScript.

Collapse
 
badgamerbad profile image
Vipul Dessai

Happy to help, however hash-map is advance, but here the problem is not that complex, so you should try it, once you start using the correct data structure, you will fall in love with them and effectively it will help you excel programming.

Collapse
 
qavsdev profile image
QAvsDEV

Nice and concise - love it