DEV Community

Cover image for JavaScript Katas: Whose Move
miku86
miku86

Posted on • Edited on

JavaScript Katas: Whose Move

Intro 🌐

I take interesting katas of all levels and explain how to solve them.

Problem solving is an important skill, for your career and your life in general.

You'd better learn to solve problems!


Source

I take the ideas for the katas from different sources and re-write them.

Today's source: Codewars


Understanding the Exercise ❗

First, we need to understand the exercise!

This is a crucial part of (software) engineering.

Go over the exercise explanation again until you understand it 100%.

Do NOT try to save time here.

My method to do this:

  1. Input: What do I put in?
  2. Output: What do I want to get out?

Today's exercise

Write a function whoseMove, that accepts two parameters: lastPlayer and isWin.

Given a string of the last player, e.g. "white",
and a string if s/he won, e.g. true,
return a string whose turn it is, e.g. "white":

  • if the last player has won, then it's his/her turn
  • if the last player has lost, then it's the opponent's turn

Input: two strings.

Output: a string.


Thinking about the Solution 💭

I think I understand the exercise (= what I put into the function and what I want to get out of it).

Now, I need the specific steps to get from input to output.

I try to do this in small baby steps.

  1. Check if the last player has won
  2. If yes (= has won), it's the last player's turn
  3. If not (= has lost), it's the opponent's turn
  4. Return the string whose turn it is

Example:

  • Input: "white", true
  • Check if the last player has won: true => it's the last player's turn
  • Output: "white"

Implementation ⛑

function whoseMove(lastPlayer, isWin) {
  // check if player has won
  return isWin
    ? lastPlayer // if yes, it's the last player's turn
    : lastPlayer === "white"
      ? "black" // if not, and the last player was white, return black
      : "white"; // if not, and the last player was black, return white
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(whoseMove("white", true));
// "white" ✅

console.log(whoseMove("black", false));
// "white" ✅
Enter fullscreen mode Exit fullscreen mode

Playground ⚽

You can play around with the code here


Next Part ➡️

Great work!

We learned how to use the ternary operator.

I hope that you can use your new learnings to solve problems more easily!

Next time, we'll solve another interesting kata. Stay tuned!


If I should solve a specific kata, shoot me a message here.

If you want to read my latest stuff, get in touch with me!


Further Reading 📖


Questions ❔

  • How often do you do katas?
  • Which implementation do you like more? Why?
  • Any alternative solution?

Top comments (3)

Collapse
 
exenestecnico profile image
Ernesto

Do this one.

Collapse
 
miku86 profile image
miku86

Hey Ernesto,

thanks for your suggestion!
Looking at the kata's grade (2 kyu) and the difference to my grade (4 kyu) and reading the discussion, currently the kata is probably too difficult for me.

But I will keep you posted when I will have managed to (not) solve the problem.

Collapse
 
exenestecnico profile image
Ernesto

I did it, I thought it was pretty cool kata. Is about about regular expressions and state machines. Did this one and this one first. Please check out my profile and leave yours so I can follow you.