JavaScript Katas: Higher Version
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-18 15:41:30

---
title: "JavaScript Katas: Higher Version"
description: "We learn how to use split, map, for, break"
published: true
tags: ["JavaScript", "Beginners", "Webdev", "Codenewbie"]
series: JavaScript Katas with miku86
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/0jfyzv0grswtx1mlo17m.jpg
---

## 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](https://www.codewars.com/kata/58884e352ffea6d2f8000082)

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
1. If higher, then return true
1. If lower, then return false
1. 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 ⛑

```js
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

```js
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](https://repl.it/@miku86/js-katas-higher-version)

---

## 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](https://forms.gle/RmQLq7CLDiD2Ptmw5).

If you want to read my latest stuff, [get in touch with me!](https://miku86.com/newsletter)

---

## Further Reading 📖

- [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- [for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [break](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break)

---

## Questions ❔

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