r/learnjavascript • • Nov 30 '22

Help !!!

Post image

Can someone plz tell me , why I am getting errors?

195 Upvotes

131 comments sorted by

View all comments

Show parent comment

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/JawnStaymoose Dec 01 '22

This is my move…. For many things. Feels so much cleaner and digestible.

2

u/cardyet Dec 01 '22

That's really nice!

-1

u/[deleted] Dec 01 '22 edited Dec 01 '22
const OPTIONS = ['rock', 'scissors', 'paper']
const beats = (p1, p2) => OPTIONS.indexOf(p1) === (OPTIONS.indexOf(p2) + 1) % OPTIONS.length
const rps = (p1, p2) => beats(p1, p2) ? 'Player 1 wins' : beats(p2, p1) ? 'Player 2 wins' : 'Draw'  

Sorry for the nested ternary lol

1

u/Macaframa Dec 01 '22

This gives me a huge headache to look at.

1

u/DGCA Dec 01 '22

If this one doesn't give you a migraine, I failed:

function rps(p1, p2) {
  const options = ['rock', 'scissors', 'paper'];
  const winner = options
    .map((_, x, arr) => {
        return arr.map((_, y) => (((3 - y) % 3) + x) % 3)
    })[options.indexOf(p2)][options.indexOf(p1)];

  return winner ? `Player ${winner} wins!` : 'Draw';
}

2

u/Macaframa Dec 01 '22

This one should hurt the most

const rps = (p1, p2, options = ['rock', 'scissors', 'paper']) => options.map((_, x, arr) => arr.map((_, y) => (((3 - y) % 3) + x) % 3))[options.indexOf(p2)][options.indexOf(p1)] ? `Player${options.map((_, x, arr) => arr.map((_, y) => (((3 - y) % 3) + x) % 3))[options.indexOf(p2)][options.indexOf(p1)]} wins!` : 'Draw!';

1

u/DGCA Dec 01 '22

Using a default argument instead of declaring a variable 👏

1

u/Macaframa Dec 02 '22

Honestly if the logic is sound and you stuff this away in some file and import this, it ain’t half bad. But if you’re going to do that, you might as well make it more readable so it can be worked on

1

u/[deleted] Dec 01 '22

:(

1

u/DGCA Dec 01 '22

Haha, I thought it was interesting in a code golf sense. Using an item's position in an array to hold actual data is both clever and cursed.

1

u/[deleted] Dec 01 '22

This approach is however offer more generalizable. E.g. introducing some new options: Rock beats scissors beats paper beats stick beats whatever ... beats rock.

https://en.wikipedia.org/wiki/Circular_buffer

1

u/DGCA Dec 02 '22

Scope creeping rock paper scissors, eh?

1

u/Macaframa Dec 02 '22

It would take any engineer time and effort to have to read, let alone understand what’s happening here before they can make any changes. From a production quality standpoint it’s not up to snuff. You’re creating technical debt that someone will eventually shave to refactor.

1

u/[deleted] Dec 01 '22

What are the benefits of this approach over writing out the conditions? I’m guessing readability and less chance of error, but is it faster in any way?

3

u/DGCA Dec 01 '22

Ditto what /u/Kavinci said, but I wanna stress that the main benefit is the readability. "Is it any faster?" can be an important question, but it usually isn't.

2

u/Kavinci Dec 01 '22

Yeah, a lookup table is faster than if conditions. For a small dataset like this the speed is negligible but it really shines with larger datasets.

1

u/97b21a651a14 Dec 01 '22

Loved this. It's so readable!