## 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 `calculateAmountOfPoints`, that accepts one parameter: `games`, a valid array of strings, e.g. `["3:1", "2:2"]`.

Points:

• x > y: 3 points
• x = y: 1 point
• x < y: 0 point

The function should return a number. The number is the sum of the point(s) of every single game.

Input: an array of strings.

Output: a number.

## 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. loop over every element of the games array
2. calculate the specific score of this element
3. return the sum of every single element (a number)

Example:

• Input: `["3:1", "2:2"]`
• Iteration 1: `"3:1"` // x > y => 3 points
• Iteration 2: `"2:2"` // x = y => 1 point
• Output: `4` // return the sum (3 + 1) as a number

## Implementation (for of-loop) ⛑

``````function calculateAmountOfPoints(games) {
// counter for total points
let totalPoints = 0;

for (const game of games) {
// split the game score for both teams and assign it to two variables
const [scoreA, scoreB] = game.split(":");

// calculate the points for this game with a ternary operator
const points = scoreA > scoreB ? 3 : scoreA === scoreB ? 1 : 0;

// add the points for this specific game to the total points
totalPoints += points;
}

}
``````

## Result

``````console.log(calculateAmountOfPoints(["3:1", "2:2"]));
// 4

console.log(calculateAmountOfPoints(["1:0", "2:0", "3:0", "4:0", "2:1"]));
// 15
``````

## Implementation (functional) ⛑

``````function calculateAmountOfPoints(games) {
return (
games
// split the game score into two separate values
.map((game) => game.split(":"))

// calculate the points for this game with a ternary operator
.map((score) => (score > score ? 3 : score === score ? 1 : 0))

// calculate the sum of all games
.reduce((acc, cur) => acc + cur, 0)
);
}
``````

## Result

``````console.log(calculateAmountOfPoints(["3:1", "2:2"]));
// 4

console.log(calculateAmountOfPoints(["1:0", "2:0", "3:0", "4:0", "2:1"]));
// 15
``````

## Playground ⚽

You can play around with the code here

## Next Part ➡️

Great work, mate!

Next time, we'll solve the next 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!