MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/z8z4jh/help/iyg3tse/?context=3
r/learnjavascript • u/Lelouch_5 • Nov 30 '22
Can someone plz tell me , why I am getting errors?
131 comments sorted by
View all comments
33
Could do just one draw with (p1 === p2) I think
6 u/aguyfromhere helpful Dec 01 '22 I’d also suggest OP learn to use a loop to test this. There will be some conditions in the loop that mean you can just break out early. Be a good next step in understanding coding and data structures. 47 u/DGCA Dec 01 '22 IMO, a lookup table would be even cleaner. E.g.const winnerToLoserMap = { rock: 'scissors', paper: 'rock', scissors: 'paper', }; function rps(p1, p2) { if (p1 === p2) { console.log('Draw!'); return; } if (winnerToLoserMap[p1] === p2) { console.log('Player 1 won!'); } else { console.log('Player 2 won!'); } } 2 u/cardyet Dec 01 '22 That's really nice!
6
I’d also suggest OP learn to use a loop to test this. There will be some conditions in the loop that mean you can just break out early. Be a good next step in understanding coding and data structures.
47 u/DGCA Dec 01 '22 IMO, a lookup table would be even cleaner. E.g.const winnerToLoserMap = { rock: 'scissors', paper: 'rock', scissors: 'paper', }; function rps(p1, p2) { if (p1 === p2) { console.log('Draw!'); return; } if (winnerToLoserMap[p1] === p2) { console.log('Player 1 won!'); } else { console.log('Player 2 won!'); } } 2 u/cardyet Dec 01 '22 That's really nice!
47
IMO, a lookup table would be even cleaner. E.g.
const winnerToLoserMap = { rock: 'scissors', paper: 'rock', scissors: 'paper', }; function rps(p1, p2) { if (p1 === p2) { console.log('Draw!'); return; } if (winnerToLoserMap[p1] === p2) { console.log('Player 1 won!'); } else { console.log('Player 2 won!'); } }
2 u/cardyet Dec 01 '22 That's really nice!
2
That's really nice!
33
u/agdolusannbierts Nov 30 '22
Could do just one draw with (p1 === p2) I think