JavaScript Katas: Well of Ideas - Easy

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


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
  2. Check if the current element is good
  3. If yes (= good), increase the counter of goods
  4. 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) ⛑

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

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

Implementation (functional) ⛑

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

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

Result

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

Playground ⚽

You can play around with the code here


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.

If you want to read my latest stuff, get in touch with me!


Further Reading 📖


Questions ❔

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