JavaScript Katas: Every possible sum of two digits
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-05 12:50:19

---
title: "JavaScript Katas: Every possible sum of two digits"
description: "We learn how to use String, split, map, for"
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, we'll have a look at our first `7 kyu` kata,
meaning we slightly increase the difficulty.

Source: [Codewars](https://www.codewars.com/kata/5b4e474305f04bea11000148)

Write a function `everyPossibleSum`, that accepts one parameter: `myNumber`.

Given a number, e.g. `1234`,
return every possible sum of two digits, e.g. `[ 3, 4, 5, 5, 6, 7 ]`.

For this example, we calculate:
`[ 1 + 2, 1 + 3, 1 + 4, 2 + 3, 2 + 4, 3 + 4 ]`

---

Input: a number.

Output: an array of numbers.

---

## 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. Get the single digits of the input number
1. Go to the first digit and find all sums with each next digit, starting from the second
1. Go to the second digit and find all sums with each next digit, starting from the third
1. Do this for every digit
1. Return the array with each sum in it

---

Example:

- Input: `1234`
- Go to the first digit and find all sums with each next digit, starting from the second: `1 + 2`, `1 + 3`, `1 + 4`
- Go to the second digit and find all sums with each next digit, starting from the third: `2 + 3`, `2 + 4`
- Do this for every digit: `3 + 4`
- Return the array with each sum in it: `[ 3, 4, 5, 5, 6, 7 ]`
- Output: `[ 3, 4, 5, 5, 6, 7 ]` ✅

---

## Implementation ⛑

```js
function everyPossibleSum(myNumber) {
  // split up number into its digits
  const split = String(myNumber) // make it a string
    .split("") // split it
    .map((digit) => Number(digit)); // convert each split char to a number

  const sums = [];

  // first number: iterate from the first to the last number
  for (let first = 0; first < split.length; first++) {

    // second number: iterate from the next number after the current first number to the last number
    for (let second = first + 1; second < split.length; second++) {

      // save the sum in the sums array
      sums.push(split[first] + split[second]);
    }
  }

  return sums;
}
```

### Result

```js
console.log(everyPossibleSum(1234));
// [ 3, 4, 5, 5, 6, 7 ] ✅

console.log(everyPossibleSum(81596));
// [ 9, 13, 17, 14, 6, 10, 7, 14, 11, 15 ] ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-every-possible-sum-of-two-digits)

---

## Next Part ➡️

Great work!

We learned how to use `String`, `split`, `map`, `for`.

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 📖

- [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String)
- [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)

---

## Questions ❔

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