JavaScript Katas: Correct the mistakes of the character recognition software
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-13 07:11:14

---
title: "JavaScript Katas: Correct the mistakes of the character recognition software"
description: "We learn how to correct the mistakes of the character recognition software with JavaScript"
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 🌐

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](https://www.codewars.com/kata/577bd026df78c19bca0002c0)

---

## 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

Character recognition software is widely used to digitize printed texts. When documents are digitized character recognition softwares often make mistakes. Your task is to correct the errors in the digitized text. You only have to handle the following mistakes:

- `S` is misinterpreted as `5`
- `O` is misinterpreted as `0`
- `I` is misinterpreted as `1`

Write a function `correctMistakes`, that accepts one parameter: `inputString`, a valid string.

The function should return a string. The string contains the `inputString` with the corrections.

---

Input: a string.

Output: a string with the corrections.

---

## 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 character
2. if the character is misinterpreted (see above), correct it
3. return the result (the corrected string)

---

Example:

- Input: `"0h!"`
- Iteration 1: `"0h!"` // next character is `0` => it is in the list of corrections => correct it (`0` becomes `O`)
- Iteration 2: `"Oh!"` // next character is `h` => it is NOT in the list of corrections => return it unchanged
- Iteration 3: `"Oh!"` // next character is `!` => it is NOT in the list of corrections => return it unchanged
- Output: `"Oh!"` // return the string

---

## Implementation (functional) ⛑

```js
function correctMistakes(inputString) {
  // organize the corrections in a human-readable object/map
  const corrections = {
    "5": "S",
    "0": "O",
    "1": "I",
  };

  return (
    inputString
      // split the string into an array of characters
      .split("")
      // check if the current character is in the corrections object
      // if it is, correct it, else return it unchanged
      .map((char) =>
        corrections.hasOwnProperty(char) ? corrections[char] : char
      )
      // join the array of characters to a string
      .join("")
  );
}
```

## Result

```js
console.log(correctMistakes("0h!"));
// "Oh!"

console.log(correctMistakes("1 l0ve m1ss1ss1pp1🧶!"));
// "I lOve mIssIssIppI🧶!"
```

---

## Implementation (regex) ⛑

```js
function correctMistakes(inputString) {
  // organize the corrections in a human-readable object/map
  const corrections = {
    "5": "S",
    "0": "O",
    "1": "I",
  };

  // use a regex (//)
  // search for 5 or 0 or 1 ([501])
  // do this for the whole input (g)
  // if it finds a mistake, run the function that takes the mistake and replaces it with the correct char from the corrections object
  return inputString.replace(/[501]/g, (char) => corrections[char]);
}
```

## Result

```js
console.log(correctMistakes("0h!"));
// "Oh!"

console.log(correctMistakes("1 l0ve m1ss1ss1pp1🧶!"));
// "I lOve mIssIssIppI🧶!"
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-correct-mistakes-recognition-software)

---

## 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](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)
- [hasOwnProperty](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
- [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
- [replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
- [replace: function as param](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter)
- [regex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
- [regex playground](https://regex101.com/)

---

## Questions ❔

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