Javascript Challenge

Javascript Challenge

Challenge 3: CODE WARS CHALLENGE

ยท

4 min read

Hey coders!!! Welcome or welcome back to the blog.

It's day 3 of this challenge and a bit slow considering today's date but y'all have to bear with me because I'm having to juggle school and this(my learning journey in tech). Like earlier stated, I took up this challenge to put me on my toes and it's been working because all I could think about today was this challenge and the blog.

With that being said, I'm back with another codewars challenge lol as a code warrior in the making๐Ÿ™ƒ๐Ÿ˜ and this challenge is going to test our logic again using the timeless game called RockPaperScissors. Pretty sure most of y'all played this game I mean I did and still do. Since we may already have an idea of how this game works, we are going straight into this challenge.

Task: Let's play! You have to return which player won! In case of a draw return Draw!.

Test & Instruction: Examples(Input1, Input2 --> Output): "scissors", "paper" --> "Player 1 won!" "scissors", "rock" --> "Player 2 won!" "paper", "paper" --> "Draw!"

I saw this challenge and immediately screamed conditionals x2 like a toddler in the candy store lol but after submitting, I saw that they were so many other ways to solve this task that includes; map, and switch, to name a few. However, I'm going to walk y'all through my solution.

Solution:

function rockPaperScissors(p1, p2) {
    if (p1 === p2) {
      return `Draw!`;
    }
    if (p1 === 'rock' && p2 === 'scissors') {
      return `Player 1 won!`;
    } else if (p1 === 'paper' && p2 === 'rock') {
      return `Player 1 won!`;
    } else if (p1 === 'scissors' && p2 === 'paper') {
      return `Player 1 won!`;
    } else {
      return `Player 2 won!`;
    }
  }

As we would usually play, if you and your partner form a closed fist or either shape together, that's an automatic tie, right? Right?! And that's the same logic I used in line 2.

Now, ordinarily when playing, the player with the strongest object wins and I simply applied the same logic to that effect. Player 1 won in line 5 because they had the strongest object and it went on and on. Now, if you look through line 11, the logic simply means "if the reverse was the case"- if player 1 didn't meet our expectations, then player 2 wins!

There's simply another way to write this conditional;

const rps = (p1, p2) => {
  if (p1 === p2) return 'Draw!';
  if (p1 === 'rock' && p2 === 'scissors') return 'Player 1 won!';
  if (p1 === 'scissors' && p2 === 'paper') return 'Player 1 won!';
  if (p1 === 'paper' && p2 === 'rock') return 'Player 1 won!';
  return 'Player 2 won!';
};

And another way;

const rps = (p1, p2) => {
  if (p1 == 'scissors' && p2 == 'paper') {
    return 'Player 1 won!'
  }
  if (p1 == 'paper' && p2 == 'rock') {
    return 'Player 1 won!'
  }
  if (p1 == 'rock' && p2 == 'scissors') {
    return 'Player 1 won!'
  }
    if (p1 == 'paper' && p2 == 'scissors') {
    return 'Player 2 won!'
  }
  if (p1 == 'rock' && p2 == 'paper') {
    return 'Player 2 won!'
  }
  if (p1 == 'scissors' && p2 == 'rock') {
    return 'Player 2 won!'
  }

  return 'Draw!'
}

The difference between the third and first is that I didn't have to use the 'else' plus it's more explanatory.

I hope all this sense, would have loved to share my solution using the map and switch method too but I still haven't had a grasp on it yet. Kindly comment on what you think about this solution, and I'm very much open to more ideas and methods to make this even better. Of course, feel free to share them in the comments or DMs, it'll be greatly appreciated! Also, to keep up with the challenge and the blog, I may have to binge-write some of these tasks and their solutions and then schedule posting because I attempt more code wars than I blog and with writing, you just need to tap into your zen mood. Anyhoo, stay safe, take breaks and stay consistent!

Write you soon!

ย