Help with switch statements!

What will be printed out when you run the following code (choose 1)?
var n = 1;
switch {
case 3: console.log(‘C’);
case 1: console.log(‘A’);
case 2: console.log(‘B’);
}

does this switch statement go in order starting from A, then B , then C?

The answer to what you are asking for can be seen here: https://www.kirupa.com/html5/switch_statements_javascript.htm

I would highly encourage you to read that first. Also, is your code snippet complete?

You would need to pass an argument to your switch statement to have it select the right answer.

Cheers,
Kirupa :laughing:

What will be printed out when you run the following code (choose 1)?
var n = 1;
switch (n) {
case 3: console.log(‘C’);
case 1: console.log(‘A’);
case 2: console.log(‘B’);
}
A. A
B. A
B
C. C
A
B
D. The code will not run because there are no break; statements
E. The code will not run because the cases are not in order

Sorry this is the correct question.

What’s your guess?

1 Like

Hassan - did you read the article I linked to earlier? It is dedicated entirely to basically answering your question :grinning:

Also, asking us to give you the answer isn’t the best way to learn.

I did, i think cause since case 1 is the first it starts there and then goes on to case 2 and then 3 in order?

Actually cause n=1 it goes straight to case 1 and since there is no break it will continue to execute what ever lies beneath it which case 2. So I think the answer is A B ?

1 Like

A B would be my answer as well for the reasons you mention :slight_smile:

I can confirm that the answer is A B, although, I have to confess I do not understand why?

I thought the code would fail after reaching “case 2” since that would be an alien bird inside the scope of “case 1” - without the break statement.

So it just ignore the “case2 :” statement then?

jsfiddle.net code

console = {
  _createConsole: function() {
var pre = document.createElement('pre');
pre.setAttribute('id', 'console');
document.body.insertBefore(pre, document.body.firstChild);
return pre;
  },
  log: function(message) {
var pre = document.getElementById("console") || console._createConsole();
pre.textContent += ['>', message, '\n'].join(' ');
  }
};

var n = 1;
switch (n) {
  case 3:
console.log('C');
  case 1:
console.log('A');
  case 2:
console.log('B');
}

Edit: Hmmm, maybe it’s a feature?

Thanks guys for your response. I have another question :

The user inputs the number and type of coffees that they require in text fields with the
ids ‘input1’ and ‘input2’ respectively and the page calculates the cost and
writes it to a text area with the id ‘result’. The functions getInput(),
setOutput() and formatMoney() are similar to many of the examples in the
course and can be expected to behave correctly.

function react() {
    'use strict';
    var number = getInput('input1');
    if (number <= 0) {
        alert('Please enter a positive number');
        return false;
    }
    var cost = 0;
    var type = document.getElementById('input2').value;
    switch (type) {
        case 'Latte':
            cost = 2.0;
            break;
        case 'Cappuccino':
            cost = 1.8;
            break;
        case 'FlatWhite':
            cost = 1.5;
            break;
        default:
            setOutput('Coffee ' + type + ' not recognised');
            return false;
    }
    if (number > 5) { // discount
        cost -= 0.2;
    }
    cost *= number;
    setOutput('That will be ' + formatMoney(cost));
    return false;
}

What is the output if the user enters “5” and “latte” in the number and type text
fields (choose 1)?
A. There will be a run-time error
B. That will be £10.00
C. That will be £9.00
D. That will be £5.00
E. Coffee latte not recognised

What is the output if the user enters “10” and “Latte” in the number and type
text fields (choose 1)?
A. There will be a run-time error
B. That will be £20.00
C. That will be £18.00
D. That will be £10.00
E. Coffee Latte not recognised

Would the answer be be D That will be £5.00 and the answer C £18.00. I’m not entirely sure at how to explain this but could someone help me explain it thanks!

Try explaining why you think that is the case! That will help you understand better and us figure out where to focus our attention :stuck_out_tongue:

well firstly, cause Latte is in the form of a case and it says cost =2.0, so if we enter 5 it will be £5.00. As for £18 i took a wild guess. I have no idea otherwise

thats not 5.

coffee latte wont be recognized?

For the first one or both?

for the first one

im mainly stuck on the second one

That’s correct for the first one.

Guessing won’t help you on your exam. At what point where you stuck where you felt the need to guess? Walk us through what you think the code is doing :slight_smile:

basically you have all the types and then i the output they are not recognized. Then the user creates a new field called latte and types in 10 since 10 is greater than 5 it does 10 minus 0.2= 9.8 and then after that I dont really know where to go. There may be a few guesses i have to make as i am a beginner and they are testing us on this stuff already which is early.