JavaScript Katas: Higher Version

Intro 🌐

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

That's why I take interesting katas of all levels, customize them and explain how to solve them.


Understanding the Exercise❗

First, we need to understand the exercise! If you don't understand it, you can't solve it!.

My personal method:

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

Today's exercise

Today, another 7 kyu kata, meaning we slightly increase the difficulty.

Source: Codewars

Write a function higherVersion, that accepts two parameters: version1 and version2.

Given two strings, e.g. "1.2.3" and "1.2.0", return if the first string is higher than the second, e.g. true.

There are no leading zeros, e.g. 100.020.003 is given as 100.20.3.


Input: two strings.

Output: a boolean.


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 current number of the first string is higher, lower or equal than/to the first number of the second string
  2. If higher, then return true
  3. If lower, then return false
  4. If equal, go to the next number of both strings and start from step 1

Example:

  • Input: "1.2.3", "1.2.0"
  • Check if the current number of the first string is higher, lower or equal than/to the first number of the second string: 1 and 1 => equal
  • If equal, go to the next number of both strings and start from step 1
  • Check if the second number of the first string is higher, lower or equal than/to the second number of the second string: 2 and 2 => equal
  • If equal, go to the next number of both strings and start from step 1
  • Check if the third number of the first string is higher, lower or equal than/to the third number of the second string: 3 and 0 => higher
  • If higher, then return true
  • Output: true βœ…

Implementation β›‘

function higherVersion(version1, version2) {
  // split the strings into numbers
  const split1 = version1.split(".").map((s) => Number(s));
  const split2 = version2.split(".").map((s) => Number(s));
  let result = null;

  for (let i = 0; i < split1.length; i++) {
    if (split1[i] > split2[i]) {
      // is higher, so break out of the whole loop
      result = true;
      break;
    } else if (split1[i] < split2[i]) {
      // is smaller, so break out of the whole loop
      result = false;
      break;
    } else {
      // is equal, so check the next number
      result = false;
    }
  }

  return result;
}

Result

console.log(higherVersion("1.2.3", "1.2.0"));
// true βœ…

console.log(higherVersion("9", "10"));
// false βœ…

Playground ⚽

You can play around with the code here


Next Part ➑️

Great work!

We learned how to use split, map, for, break.

I hope 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?