DEV Community

Cover image for Codewars Get Planet Name By ID JavaScript
Dylan Attal
Dylan Attal

Posted on

Codewars Get Planet Name By ID JavaScript

This article explains how to solve the kata Get Planet Name By ID from Codewars in JavaScript.

Instructions

The function is not returning the correct values. Can you figure out why?
Example (Input --> Output ):
3 --> "Earth"

Provided Code

function getPlanetName(id){
  var name;
  switch(id){
    case 1:
      name = 'Mercury'
    case 2:
      name = 'Venus'
    case 3:
      name = 'Earth'
    case 4:
      name = 'Mars'
    case 5:
      name = 'Jupiter'
    case 6:
      name = 'Saturn'
    case 7:
      name = 'Uranus'
    case 8:
      name = 'Neptune'
  }

  return name;
}
Enter fullscreen mode Exit fullscreen mode

Discussion

This kata is testing our knowledge about switch statements.

Switch statements are cool because they provide an easily readable syntax for times when a condition is checked against many different possible cases. Oftentimes, switch statements can save you from typing out many if statements.

Let's take a look at a series of if statements that can be converted into one switch statement:

const favoriteFood = "pizza";

if (favoriteFood === "ice cream") {
    return "Ice cream is my favorite food!";
} else if (favoriteFood === "potato chips") {
    return "My favorite food is potato chips!";
} else if (favoriteFood === "pizza") {
    return "I love pizza sooooo much!";
} else if (favoriteFood === "gummy bears") {
    return "My favorite food is gummy bears!";
} else {
    return "Name of food not recognized";
}
Enter fullscreen mode Exit fullscreen mode

The above program returns a sentence about your favorite food based on whether it is ice cream, potato chips, gummy bears, pizza, or something else.

Let's see how much more succinct the program appears when we use a switch statement:

const favoriteFood = "pizza";
let sentence = "";

switch (favoriteFood) {
    case "ice cream":
        sentence = "Ice cream is my favorite food!";
        break;
    case "potato chips":
        sentence = "My favorite food is potato chips!";
        break;
    case "pizza":
        sentence = "I love pizza sooooo much!";
        break;
    case "gummy bears":
        sentence = "My favorite food is gummy bears!";
        break;
    default:
        sentence = "Name of food not recognized";
}

return sentence;
Enter fullscreen mode Exit fullscreen mode

We don't even need the local variable sentence in the above code. We can just return the sentence that we want straight from within the switch statement:

const favoriteFood = "pizza";

switch (favoriteFood) {
    case "ice cream":
        return "Ice cream is my favorite food!";
    case "potato chips":
        return "My favorite food is potato chips!";
    case "pizza":
        return "I love pizza sooooo much!";
    case "gummy bears":
        return "My favorite food is gummy bears!";
    default:
        return "Name of food not recognized";
}
Enter fullscreen mode Exit fullscreen mode

Switch statements take an expression and then look for the first case clause whose expression is the same.

In the above example, the switch expression is given the variable favoriteFood which we see is earlier set to "pizza". We would expect that switch statement to return "I love pizza sooooo much!".

Nota Bene: you should end each case clause with break. This tells the program to exit the switch statement because a matching case has been found. If you forget to write break all the cases after the matching case will be run in addition to the correct case. However, break is not necessary if you have a return statement because return will exit the switch statement as well.

One last note: it is optional to add a default clause to the end of your switch statement that will run if no case matches the provided expression. This can useful for catching unexpected values and helping track down bugs.

Solution 1

The crux of the issue in the given code in this kata is that break is missing from each case in the switch statement. Therefore, we can solve this problem by just adding break at the end of each case.

function getPlanetName(id) {
  var name;
  switch(id) {
    case 1:
      name = 'Mercury';
      break;
    case 2:
      name = 'Venus';
      break;
    case 3:
      name = 'Earth';
      break;
    case 4:
      name = 'Mars';
      break;
    case 5:
      name = 'Jupiter';
      break;
    case 6:
      name = 'Saturn';
      break;
    case 7:
      name = 'Uranus';
      break;
    case 8:
      name = 'Neptune';
      break;
  }

  return name;
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

Another solution is to forget about using break altogether and instead just return the name of each planet straight from the switch statement. I like how concise this solution is.

This solution also includes a default clause in case an id passed to our function getPlanetName doesn't match any of the cases we defined, for example, if we called getPlanetName(11) then we'd expect the function to return "Invalid planet id".

function getPlanetName(id) {  
  switch(id) {
    case 1:
      return "Mercury";
    case 2:
      return "Venus";
    case 3:
      return "Earth";
    case 4:
      return "Mars";
    case 5:
      return "Jupiter";
    case 6:
      return "Saturn";
    case 7:
      return "Uranus";
    case 8:
      return "Neptune";
    default:
      return "Invalid planet id";
  }
}
Enter fullscreen mode Exit fullscreen mode

RIP Pluto.


Hope you enjoyed the article!
Follow me on LinkedIn and GitHub!

Top comments (0)