JavaScript Katas: Count lowercase letters
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-28 15:42:00

---
title: "JavaScript Katas: Count lowercase letters"
description: "We learn how to use for of, match, filter and length."
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

Source: [Codewars](https://www.codewars.com/kata/56a946cd7bd95ccab2000055)

Write a function `amountOfLowercaseLetters`, that accepts one parameter: `inputString`.

Given a string, e.g. `"aB1c"`,
return the number of lowercase letters in this string, e.g. `2`:

---

Input: a string.

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 character
1. Check if it is a lowercase letter
1. If yes, then increase count of lowercase letters by 1
1. Return count of lowercase letters

---

Example:

- Input: `"aB1c"`
- Iteration 1: lowercase letter? `true` => Increase count of lowercase letters by 1
- Iteration 2: lowercase letter? `false` => Do nothing
- Iteration 3: lowercase letter? `false` => Do nothing
- Iteration 4: lowercase letter? `true` => Increase count of lowercase letters by 1
- Output: `2` (count of lowercase letters) ✅

---

## Implementation (for) ⛑

```js
function amountOfLowercaseLetters(inputString) {
  let count = 0;

  // loop over every char
  for (const char of inputString) {
    // check if it is lowercase
    if (char.match(/[a-z]/)) {
      // if yes, increase count
      count += 1;
    }
  }

  return count;
}
```

### Result

```js
console.log(amountOfLowercaseLetters("aB1c"));
// 2 ✅

console.log(amountOfLowercaseLetters("123"));
// 0 ✅
```

---

## Implementation (functional) ⛑

```js
function amountOfLowercaseLetters(inputString) {
  return inputString
    .split("") // convert into array
    .filter((char) => char.match(/[a-z]/)) // filter out all lowercase chars
    .length; // take the length
}
```

### Result

```js
console.log(amountOfLowercaseLetters("aB1c"));
// 2 ✅

console.log(amountOfLowercaseLetters("123"));
// 0 ✅
```

---

## Implementation (global regex) ⛑

```js
function amountOfLowercaseLetters(inputString) {
  return (inputString.match(/[a-z]/g) || []).length;
}
```

We use the [`g` flag to return all matches](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Return_value). Because `null` would get returned if there wouldn't be a match, we add an `[]` to use `length`.

### Result

```js
console.log(amountOfLowercaseLetters("aB1c"));
// 2 ✅

console.log(amountOfLowercaseLetters("123"));
// 0 ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-count-lowercase-letters)

---

## Next Part ➡️

Great work!

We learned how to use `for of`, `match`, `filter` and `length`.

I hope that 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 📖

- [for of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
- [match](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
- [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
- [length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)

---

## Questions ❔

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