β Blog## JavaScript Katas: Calculate total amount of points

## Intro π

## Source

## Understanding the Exercise β

### Today's exercise

## Thinking about the Solution π

## Implementation (for of-loop) β

## Result

## Implementation (functional) β

## Result

## Playground β½

## Next Part β‘οΈ

## Further Reading π

## Questions β

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!

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

Today's source: Codewars

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:

- Input: What do I put in?
- Output: What do I want to get out?

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.

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.

- loop over every element of the games array
- calculate the specific score of this element
- 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

```
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;
}
return totalPoints;
}
```

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

```
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[0] > score[1] ? 3 : score[0] === score[1] ? 1 : 0))
// calculate the sum of all games
.reduce((acc, cur) => acc + cur, 0)
);
}
```

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

You can play around with the code here

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!

- Do you like to solve katas?
- Which implementation do you like more? Why?
- Any alternative solution?

Hi! I'm Michael π I'm a Mentor & Senior Web Developer - I help you to reach your (career) goals.