# AP Lab Switch not working?

**URL:** <https://forum.code.org/t/ap-lab-switch-not-working/11830>\
**Category:** CS Principles\
**Created:** [November 8, 2017, 6:35pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830 "2017-11-08T18:35:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthony\_barbuto](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anthony\_barbuto](https://forum.code.org/u/anthony_barbuto)\
**Post date:** [November 8, 2017, 6:35pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/1 "2017-11-08T18:35:44Z")

</div>

I wrote a program to do conversions from decimal to binary, hex,octal and vice versa last year while my students were engaged in the AP performance tasks for AP CSP. I tried to run it and found that it no longer works as designed. After placing some console log statements in various functions I found that the **switch** statement no longer works. I replaced my switches with _ **if then elses** _ and it works. I am curious why this changed? It looks like the switch block was taken out of the toolbox.

---

<div class="post-metadata">

**Author:** ![frank\_w\_lee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/frank_w_lee/32/4895_2.png) [@frank\_w\_lee](https://forum.code.org/u/frank_w_lee)\
**Post date:** [November 9, 2017, 3:47am UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/2 "2017-11-09T03:47:33Z")

</div>

Hm, I’ve never noticed the switch statement in there, but maybe it’s because I never used it as part of the course (either because it wasn’t in the curriculum or I didn’t get to that part of the curriculum).

I’ll forward your question to [code.org](http://code.org) staff.

---

<div class="post-metadata">

**Author:** ![baker](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/baker/32/1229_2.png) [@baker](https://forum.code.org/u/baker)\
**Post date:** [November 9, 2017, 4:13am UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/3 "2017-11-09T04:13:01Z")

</div>

Hi Anthony,

Sorry, I noticed this post this morning and brought it up with our engineers. Switch statements should work, but, guess what? You exposed a bug, but it wasn’t our fault! Our engineer discovered something in our code interpreter - a 3rd party had implemented some logic incorrectly. Our engineer fixed it, and it should go out with tomorrow’s deploy.

Also, I don’t think switch statements were ever in our code toolbox. You can use them because they are part of javascript. And they _should_ work - and they actually do now in most cases, except for what our engineer found. If you’re interested here was the code he used to reveal the problem:

```auto
function doSwitch(x) {
  switch(x) {
    case 1:
      console.log('case 1');
      break;
    case 2:
      console.log('case 2');
      break;
    case 3:
      console.log('case 3');
      break;
    case 4:
      console.log('case 4');
      break;
    default:
      console.log('case 5');
  }
}

// This works
// Expected&Actual: case 3
doSwitch(3); 

// Either loop or switch is broken
// Expected: case 1 case 2 case 3 case 4 case 5
// Actual : case 1 case 1 case 1 case 1 case 1
for (var i = 1; i <= 5; i++) {
  doSwitch(i);
}

```

Anyway, thanks for the find. But switch statements should work.

---

<div class="post-metadata">

**Author:** ![ericwolgie](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ericwolgie](https://forum.code.org/u/ericwolgie)\
**Post date:** [November 15, 2019, 3:56pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/4 "2019-11-15T15:56:23Z")

</div>

This appears to still be a bug. Several of my students used switch (this week) and had their code bomb and had to rewrite it using if elses.

---

<div class="post-metadata">

**Author:** ![frank\_w\_lee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/frank_w_lee/32/4895_2.png) [@frank\_w\_lee](https://forum.code.org/u/frank_w_lee)\
**Post date:** [November 17, 2019, 1:38am UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/5 "2019-11-17T01:38:55Z")

</div>

Hi @ericwolgie,

Would it be possible for you to link to some live samples? That way I can submit them to [code.org](http://code.org) staff for analysis.

Thanks!

---

<div class="post-metadata">

**Author:** ![ericwolgie](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ericwolgie](https://forum.code.org/u/ericwolgie)\
**Post date:** [November 18, 2019, 2:05am UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/6 "2019-11-18T02:05:32Z")

</div>

Here’s a simple example

[https://studio.code.org/projects/applab/prpjHU7HSDG46z\_VkDE\_e7a2VWMf4q\_sncZ5oLyAh8I](https://studio.code.org/projects/applab/prpjHU7HSDG46z_VkDE_e7a2VWMf4q_sncZ5oLyAh8I)

Notice that if the user hits any key it works on the first pass as expected. EVERY other pass through the switch case statements will run the first (case “Up”) case, regardless if that key was pressed or not.

Thanks!

I have some advanced students who have already taken our beginning programming and AP Java classes, and I’m encouraging them to prototype their JavaScript code in app lab before moving over to execute code on our classroom web pages using  
html, javacripts and (eventually) css.

I _ **required** _ that they use case switch and now I got me some egg on my face  
😊

Mail]([https://go.microsoft.com/fwlink/?LinkId=550986](https://go.microsoft.com/fwlink/?LinkId=550986)) for Windows 10

---

<div class="post-metadata">

**Author:** ![frank\_w\_lee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/frank_w_lee/32/4895_2.png) [@frank\_w\_lee](https://forum.code.org/u/frank_w_lee)\
**Post date:** [November 18, 2019, 4:09pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/7 "2019-11-18T16:09:20Z")

</div>

Thanks for sending in the sample! I notified [code.org](http://code.org) staff and they said it is indeed still broken. Unsure when it will be fixed, but it’s not high on the priority list compared to other to-dos that are on the curriculum or in the toolbox. Sorry!

---

<div class="post-metadata">

**Author:** ![ericwolgie](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ericwolgie](https://forum.code.org/u/ericwolgie)\
**Post date:** [November 18, 2019, 5:22pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/8 "2019-11-18T17:22:28Z")

</div>

Thanks for the quick response!

Would it be fair to say that the existing blocks are good to go, but using java-script commands/code that hasn’t been ‘blockiefied’ is sort of a use-at-your-own-risk kinda thing?

Mail]([https://go.microsoft.com/fwlink/?LinkId=550986](https://go.microsoft.com/fwlink/?LinkId=550986)) for Windows 10

---

<div class="post-metadata">

**Author:** ![frank\_w\_lee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/frank_w_lee/32/4895_2.png) [@frank\_w\_lee](https://forum.code.org/u/frank_w_lee)\
**Post date:** [November 18, 2019, 8:48pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/9 "2019-11-18T20:48:20Z")

</div>

That sounds like a fair interpretation. 🙂

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [February 17, 2020, 8:25pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/10 "2020-02-17T20:25:32Z")

</div>

Switch is still not working. Don’t use it.

---

<div class="post-metadata">

**Author:** ![patrick.cummins](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@patrick.cummins](https://forum.code.org/u/patrick.cummins)\
**Post date:** [November 9, 2022, 5:51pm UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/11 "2022-11-09T17:51:05Z")

</div>

Tried to do this in App Lal

switch(clicks)  
{  
case clicks \>= 30: setProperty(“star”, “icon-color”, “pink”);  
break;  
case clicks \>= 25 && clicks \< 30: setProperty(“star”, “icon-color”, “violet”);  
break;  
case clicks \>= 20 && clicks \< 25: setProperty(“star”, “icon-color”, “blue”);  
break;  
case clicks \>= 15 && clicks \< 20: setProperty(“star”, “icon-color”, “black”);  
break;  
case clicks \>= 10 && clicks \< 15: setProperty(“star”, “icon-color”, “yellow”);  
break;  
case clicks \>= 5 && clicks \< 10: setProperty(“star”, “icon-color”, “orange”);  
break;  
case clicks \>= 1 && clicks \< 5: setProperty(“star”, “icon-color”, “red”);  
break;  
case clicks == 0 && clicks \< 1: setProperty(“star”, “icon-color”, “black”);  
break;  
}  
});

but it always defaults to the red color???

---

<div class="post-metadata">

**Author:** ![madeline\_r\_burton](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/madeline_r_burton/32/5037_2.png) [@madeline\_r\_burton](https://forum.code.org/u/madeline_r_burton)\
**Post date:** [November 10, 2022, 3:12am UTC](https://forum.code.org/t/ap-lab-switch-not-working/11830/12 "2022-11-10T03:12:36Z")

</div>

It looks like this form of control flow is not currently in the AppLab sandbox. I encourage you to send a ticket to support requesting the feature if it is something you value and would like to use!
