# Koan Wars: Kotlin vs TypeScript - Scrabble

## Introduction

Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript.

In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper understanding of the key features and nuances of both Kotlin and TypeScript so that you can apply this knowledge to your own programming projects.

Whether you're a beginner or an experienced programmer, Koans provide an excellent way to learn a new language or deepen your knowledge of an existing one. Through these Koan Battles, we'll explore the similarities and differences between Kotlin and TypeScript, and discover the unique strengths of each language.

So join me as we embark on this exciting journey of discovery, enlightenment, and er fighting!

## Today's Koan - Scrabble

Given a word, compute the Scrabble score for that word.

### Letter Values

You'll need these:

```text
Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
```

## Kotlin Solution

```kotlin
    private fun scoreLetter(letter: Char): Int = when (letter) {
        'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' -> 1
        'D', 'G' -> 2
        'B', 'C', 'M', 'P' -> 3
        'F', 'H', 'V', 'W', 'Y' -> 4
        'K' -> 5
        'J', 'X' -> 8
        'Q', 'Z' -> 10
        else -> 0
    }

    fun score(word: String): Int =
        word.uppercase().sumOf(::scoreLetter)
```

### `score` Function

The `score` function takes a single argument, a string representing the word to be scored. The function first converts the entire word to uppercase using the `uppercase()` function to make it easier to compare each character with the rules defined in `scoreLetter`. The function then uses the `sumOf` function to calculate the total score of the word by applying the `scoreLetter` function to each character in the word, and summing the results.

### `scoreLetter` Function

The `scoreLetter` function takes a single character as an argument and returns an integer value representing the point value of that character. The function uses a `when` expression to determine the point value of the letter based on its Scrabble score for that letter.

## TypeScript Solution

```typescript
const SCORE_LETTER: { [key: string]: number } = {
    A: 1, E: 1, I: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1,
    D: 2, G: 2,
    B: 3, C: 3, M: 3, P: 3,
    F: 4, H: 4, V: 4, W: 4, Y: 4,
    K: 5,
    J: 8, X: 8,
    Q: 10, Z: 10
}

export function score(word = ""): number {
    return word.toUpperCase()
        .split('')
        .reduce((accScore, letter) =>
            accScore + SCORE_LETTER[letter], 0);
}
```

### `SCORE_LETTER` Map Object

The `SCORE_LETTER` object maps each letter to the number of points it is worth. For example, "A" is worth 1 point, "D" is worth 2 points, and so on.

### `score` Function

The `score` function takes a single argument, a string representing the word to be scored. The function first converts the entire word to uppercase using the `uppercase()` function to make it easier to look up the score of each letter from the map defined in `SCORE_LETTER`.

Then, it uses the `split` method to convert the string of letters into an array of individual letters. It then calls the `reduce` method on this array to sum the score for each letter.

## Comparison

The main difference between the two:

* Kotlin uses the `when` expression while TypeScript uses object mapping to score the letters.
    
* Kotlin uses the `sumOf` function to get the overall score while TypeScript uses `reduce`.
    

## Result

I'm going to give this one to Kotlin, I love Kotlin's `when` expression and wish we had something as powerful in TypeScript. The `sumOf` is also easier on the eye than the `reduce` function. I see a pattern forming here, maybe the Kotlin standard library is just too strong!

## Score

Kotlin 2, TypeScript 0
