# Help with switch statements!

**URL:** <https://forum.kirupa.com/t/help-with-switch-statements/635773>\
**Category:** programming\
**Created:** [January 6, 2017, 9:34pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773 "2017-01-06T21:34:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 6, 2017, 9:34pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/1 "2017-01-06T21:34:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 7, 2017, 1:47am UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/2 "2017-01-07T01:47:49Z")

</div>

The answer to what you are asking for can be seen here: [https://www.kirupa.com/html5/switch\_statements\_javascript.htm](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 😆

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 7, 2017, 12:14pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/3 "2017-01-07T12:14:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alltom](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/alltom/32/7274_2.png) [@alltom](https://forum.kirupa.com/u/alltom)\
**Post date:** [January 7, 2017, 5:34pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/4 "2017-01-07T17:34:54Z")

</div>

What’s your guess?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 7, 2017, 8:07pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/5 "2017-01-07T20:07:25Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 7, 2017, 10:12pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/6 "2017-01-07T22:12:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 7, 2017, 10:17pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/7 "2017-01-07T22:17:47Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 8, 2017, 6:28am UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/8 "2017-01-08T06:28:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [January 9, 2017, 9:10am UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/9 "2017-01-09T09:10:21Z")

</div>

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](http://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?

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 11:31am UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/10 "2017-01-09T11:31:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 11:33am UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/11 "2017-01-09T11:33:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 9, 2017, 9:46pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/12 "2017-01-09T21:46:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 9:56pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/13 "2017-01-09T21:56:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [January 9, 2017, 10:01pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/14 "2017-01-09T22:01:05Z")

</div>

> [@Hassan\_Ahmed](#):
>
> cost \*= number;

thats not 5.

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 10:03pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/15 "2017-01-09T22:03:37Z")

</div>

coffee latte wont be recognized?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 9, 2017, 10:11pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/16 "2017-01-09T22:11:27Z")

</div>

For the first one or both?

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 10:12pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/17 "2017-01-09T22:12:08Z")

</div>

for the first one

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 10:12pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/18 "2017-01-09T22:12:40Z")

</div>

im mainly stuck on the second one

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 9, 2017, 10:13pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/19 "2017-01-09T22:13:41Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Hassan\_Ahmed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hassan_ahmed/32/7921_2.png) [@Hassan\_Ahmed](https://forum.kirupa.com/u/Hassan_Ahmed)\
**Post date:** [January 9, 2017, 10:18pm UTC](https://forum.kirupa.com/t/help-with-switch-statements/635773/20 "2017-01-09T22:18:29Z")

</div>

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.

[Next page](https://forum.kirupa.com/t/help-with-switch-statements/635773.md?page=2)
