Understanding Case statement syntax

Understanding Case statement syntax
0

Tell us what’s happening:
Initially, for my case statement, I had case “1”, case “2” and it didn’t work. I had my case values in quotes, as shown in the example. However, after watching tutorial video, the case statements in that example did not have quotes around the values. My question, is why did it not work with the quotes?

Your code so far


function caseInSwitch(val) {
var answer = "";
// Only change code below this line
switch(val){
case 1:
answer = "alpha";
break;

case 2:
answer = "beta";
break;

case 3:
answer = "gamma";
break;

case 4:
answer = "delta";

}
// Only change code above this line
return answer;
}

caseInSwitch(1);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

If you add quotes, then you are checking if your value is the same as the string "1" while you want to check if your value is the same as the number 1.

On a side note, I recommend using object lookups over switch statements.

I like object lookups as switches, but I prefer Python’s implementation of handling implementing the default case, personally.

@JeremyLT

Like this?

const color = {
    'black': '#000',
    'white': '#fff',
};

Object.prototype.get = function(color, _default = null){
    return this[color] || _default;  
};

console.log(color.get('black', '#fff'));

Perhaps more accurate:

const color = {
    'black': '#000',
    'white': '#fff',
};

const arr = [1,2,3];

Object.prototype.get = function(color, _default = null){
    if (Object.prototype.toString.call(this) === '[object Object]')
        return this[color] || _default;
    throw 'get method only works with objects';
};

console.log(color.get('black', '#fff'));

Yeah, that’ll work. Though you have to be careful if you have falsey values in your object lookup.

I still like Python’s handling of default cases better. In Python you just add the optional default key value argument in your dictionary lookup. Nothing else required. No lookup function. No pipes, ternary, conditionals, etc.

Though, I think we’re probably getting off topic at this point.

The switch statement does strict equality checking (i.e. no type coercion).

Description

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, executing the associated statements.

1 Like
const x = {
    0: 'hi'
};

// prints hi
console.log('numerical 0 index: ', x[0]);
// prints hi (even though '0' does not exist. javascript converts all keys to strings in object literals)
console.log('string 0 index: ', x['0']);

// map does not convert keys to strings, thus addresses the problem lasjorg points out.
const z = new Map([
    [0, 'hi']
]);

// does not print hi, because '0' does not exist
console.log(z.get('0'));
// prints hi
console.log(z.get(0));

This is of course all off topic but I’m sure educational to readers.

My reasoning for using lookups over switch statements is speed. Switch statements are technically O(n) whereas lookups are O(1) (provided no collisions). Also they are easier to read.

OP is a new student who just started learning switches after just learning if-else statements. The merits of switches vs lookups are interesting but not sanguine to a topic about giving the OP help on introductory lessons about control structures.

Thanks for the answers everyone.

1 Like