JavaScript Katas: Well of Ideas - Easy
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-23 20:11:04

---
title: "JavaScript Katas: Well of Ideas - Easy"
description: "We learn how to use for of, ternary operator and filter"
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/57f222ce69e09c3630000212)

---

## 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 `getStatusOfWellOfIdeas`, that accepts one parameter: `inputArray`.

Given an array of strings, e.g. `["bad", "good", "bad"]`, return a message string of the status of the well of ideas:

- if the well of ideas includes 0 "good": `"Fail!"`
- if the well of ideas includes 1 or 2 "good": `"Publish!"`
- if the well of ideas includes more than 2 "good" => `"I smell a series!"`

---

Input: an array of strings.

Output: a string.

---

## 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 array element
1. Check if the current element is `good`
1. If yes (= `good`), increase the counter of `good`s
1. Return the result string depending on the counter

---

Example:

- Input: `["bad", "good", "bad"]`
- Iteration 1: `"bad"` is `"good"`? => No => not increase counter => 0
- Iteration 2: `"good"` is `"good"`? => Yes => increase counter => 1
- Iteration 3: `"bad"` is `"good"`? => No => not increase counter => 1
- Output: `"Publish!"` (because 1 `good`) ✅

---

## Implementation (for loop) ⛑

```js
function getStatusOfWellOfIdeas(inputArray) {
  let goodCounter = 0;

  for (const idea of inputArray) {
    if (idea === "good") {
      goodCounter += 1;
    }
  }

  return goodCounter === 0
    ? "Fail!"
    : goodCounter > 2
      ? "I smell a series!"
      : "Publish!";
}
```

## Result

```js
console.log(getStatusOfWellOfIdeas(["bad", "good", "bad"]));
// "Publish!" ✅
```

---

## Implementation (functional) ⛑

```js
function getStatusOfWellOfIdeas(inputArray) {
  const goodCounter = inputArray.filter((idea) => idea === "good").length;

  return goodCounter === 0
    ? "Fail!"
    : goodCounter > 2
      ? "I smell a series!"
      : "Publish!";
}
```

## Result

```js
console.log(getStatusOfWellOfIdeas(["bad", "good", "bad"]));
// "Publish!" ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-well-of-ideas-easy)

---

## Next Part ➡️

Great work!

We learned how to use for of, ternary operator and filter.

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)
- [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
- [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

---

## Questions ❔

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