Help with switch statements!

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.

Yes, I didnā€™t spot that, ā€œlatteā€ is not recognised as ā€œLatteā€

cost is manipulated before multiplikation in the code.

(?) It seems you donā€™t understand *=

cost *= number;
cost = cost * number;
cost = 1.8 * 10;

1 Like

But where did you get 1.8 from?

Same as with the multiplication.

ā€œLatteā€
cost = 2.0,
number = 10 (Discount)
=>

cost -= 0.2
cost = cost - 0.2
cost = 1.8

Guys I had my test today I was wandering if I can post the questions and you can reveal to me the answers so i can check against mine?

Iā€™m guessing, but with a test this early in the semester, isnā€™t it possible the teacher just want to check the overall knowledge and plan their teaching?

Seems kind of hard to start of like this and then increase the level from here?

Yeah! Feel free to post the questions. Curious to see what is on it :slight_smile: