Strict conditionals in the lesson "Selecting from Many Options with Switch Statements"

I have a real issue with this lesson as how much emphasis was placed on strict conditionals and yet to solve the challenge you don’t use them at all.

I was thinking I had to write something like this:

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch caseInSwitch(val) {
    case === 1:
      answer = "alpha";
      break;
    case === 2:
      answer = "beta";
      break;
    case === 3:
      answer = "gamma";
      break;
    case === 4:
      answer = "delta";
      break;
  }
  
  // Only change code above this line  
  return answer;  
}

There are two problems here. For one, there are syntax errors that will cause the code to fail. Also, I had no way of knowing that I shouldn’t call the function caseInSwitch() inside my switch statement, instead switch(val) is the correct syntax.

If strict conditionals are important, where would they go?

On a side note, this challenge drove me a little crazy because of how little instruction you are given about switch statements before expecting to create your own.

It’s OK, we all get to point where something drives us crazy. I was where you are two years ago. After two years of hard work, I now do this for a living. YMMV.

I’m not sure what you mean by “strict conditional”. Do you mean using=== instead of ==? I’ll get to that in a second.

Also, I had no way of knowing that I shouldn’t call the function caseInSwitch() inside my switch statement, instead switch(val) is the correct syntax.

You have the function calling itself. That is an advanced technique called “recursion” and is definitely not what you want here. The prototype they offer has:

switch(num) {
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
...
  case valueN:
    statementN;
    break;
}

Notice that it is not calling a function there, just using a value, in this case the variable num.

If strict conditionals are important, where would they go?

That is the other problem with the code presented. Notice in the example. it’s case: value1, not case: conditional1. With an if statement you can do conditionals and complex calculations. With switch it is just one, single value (or something that evaluates to a single value). So you want case: 1. Also notice the placement of the colon.

Yes, I know it’s confusing. FCC is not comprehensive. You are not expected to use only FCC. My first instinct, if I’m not sure about something is to google it. MDN is a great resource, google “MDN switch”. As a professional developer, it’s a good day if I only have to google something 10 times. Being able to find things in the online docs, Stack Overflow, etc. - these are extremely important skills for a dev. Youtube can be great too - I bet if you searched for “javascript switch” you’d find some people walking you through it.

Don’t get frustrated. This is hard stuff. If it were easy, then anyone could do it and it would pay minimum wage. The wages are high because most people don’t have the patience to learn.

If you are still stuck, let us know. When I make those changes to your code, it passes for me.

1 Like

After a weekend letting it sink in and then coming back to it (I jumped back a few lessons just so I could review what I learned last week), I think it has sunk in, the syntax, the way it works, and everything.

Tell me if this thinking is correct: While you would not use a strict comparison operator (===) inside a switch statement (because it’s not an if statement like you said), the case part of the switch statement acts in a similar manner to a strict comparison operator. Meaning that anything assigned as a case must be exactly the same as the value inside a function’s argument, otherwise you get an error or a default statement, provided one has been assigned.

That is correct, the case works as a strict equality to that one value. Essentially, the functionality of a switch could be duplicated with a chain of if/else with only one strict equality in each. switch is just a shortcut for that and is a lot cleaner and easier to read.

From the MDN docs:

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, === ) and transfers control to that clause, …

When in doubt, check MDN.

1 Like

Nice, thanks for the helpful explanation.

That’s why we’re here. In six months, you’ll be explaining things to the next crop.

1 Like