Design comparison
Solution retrospective
determine the winner,too many "ifs" I feel a "switch" would've been the same. Obviously there is a lot to compare but is there a cleaner way? Also I see the box shadow is different, I couldn't get the correct colors! Overall great practice. feedback on how I can improve is appreciated.Thanks
Community feedback
- @patricktouchettePosted almost 5 years ago
Hi, good job on your solution. You are right that there are many if/elses. As you can see it gets confusing quickly :D. I would suggest using an extension called prettier in VScode, it can be setup to format on save. This will tremendously help code readability.
You can simplify your logic by making use of arrays and objects. Here's how I would do it. It is by no means the best way, could probably be even more concise and cleaner.
// Get player pick (this should be an input, not hardcoded) const playerPick = 'rock'; // Get random pick for computer const choices = ['rock', 'paper', 'scissor']; const computerPick = choices[Math.floor(Math.random() * 3)]; // Declare the ruleset as an object for win conditions // rock beats scissor // paper beats rock // sicssor beats paper const rules = { rock: 'scissor', paper: 'rock', scissor: 'paper', }; // check if player won, lost or got a draw const compare = () => { if (playerPick === computerPick) return 'draw'; if (rules[playerPick] === computerPick) return 'win'; return 'lose'; }; console.log('playerPick', playerPick); console.log('computerPick', computerPick); console.log(compare());
1
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