Debugging Checklist : Switch Statement in JavaScript
Your Debugging Kitty buddy

Debugging Checklist : Switch Statement in JavaScript

As a programming newbie, Kittycat prepares this Debugging Checklist for Switch Statements, just in case it gets handy again.

Given Task: 

You want to buy a treat for his majesty Meow, who is a royalty cat. However, his majesty is very picky in terms of choosing his treat. Meow asked you to code these options so you don't forget his specific choices.

In the near future, you can just check his majesty's choice of treat by calling a function that will tell you if the input treat is acceptable by his majesty or not. Here is the function pre-coded for his majesty (you can click the image itself):

No alt text provided for this image

This code took a bit of time to debug, due to common minor mistakes such as semicolon etc.

Now we want to make a debugging checklist so next time we code a switch statement, we save time and ease the pain of frustrations. Here you go:

Debugging Checklist

1) Make sure you declare the switch statement properly. Check for:

  • 'switch' +parameter + curly braces

switch(input){ 

}        

2) Inside the curly braces, time to code the 'case', 'return', 'break' block set properly.

case 'EdelCat': 
   return 'Meow Approves. Buy it!';
break        

Important To Check:

  • If a value being used is a type of string, make sure you enclosed it with ' ' single quotes.
  • Notice that in the case line of code, the line should end with a holy colon.
  • In the return line of code, the line should end with a semicolon. This line is indented.
  • In the break line of code, the line should be as it is, just put "break"

3) When repeating the case blocks, just repeat them as much as you want and MAKE SURE you don't miss any semicolon or colon that is needed. Make sure that the return code of line in the block is properly indented too.

4) Now we go to the optional case, which is the default line of code.

default :
      	return 'His Majesty Meow is very picky and does not eat this sort of cheap treats';        

  • It is almost the same as 'case' line of code, but make sure that this is NOT followed by a 'break' line.
  • It should be just 2 lines : the default line and the return line of code

5) Make sure that all the needed curly braces as always are existing, to avoid dilemma. Some newbies tend to forget this a lot of times after being so much focused on the 'case' lines.

 If you get stuck with your codes:

1) Read the console. It will tell you which mistake you have done like this one.

No alt text provided for this image
No alt text provided for this image

When I removed this wrong line of code, my code started working. You can see that the console is highlighting your code mistake, which is really a huge help for programmers especially to newbies like us.

2) Always check the documentation and have this habit of reading properly so you can avoid confusions. Here is the documentation link for Switch Statements.

3) You can get your ducky ducky to help you from debugging. For more information about the Rubber Duck effect, check this link: Software debugging.

4) If everything still does not work, you can try to reach out for help from other programmers. The good thing about the programming community is that everyone lifts each other up so you should not be scared. Try and try to reach out for help until somebody helps you.

5) Worst to worst, you can go to mentorcruise.com to get a mentor. There are a lot of highly qualified mentors out there who could be your mentor.

Just always remember, whatever happens, don't give up on your dream to be a programmer. Fight for your dream because nobody will do it for you.

To end this article , Meow would like to say that DON'T ever throw your laptop out of the window when you feel frustrated. Try to find a way how to relax and go back to it later. It's okay, everyone has been there and remember that the pain you feel now is a sign of growth! Keep growing meow! Cheers!🍺🍺

To view or add a comment, sign in

Others also viewed

Explore content categories