AP Lab Switch not working?

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.

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 staff.

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:

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.

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.

Hi @ericwolgie,

Would it be possible for you to link to some live samples? That way I can submit them to code.org staff for analysis.

Thanks!

Here’s a simple example

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
:blush:

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

Thanks for sending in the sample! I notified 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!

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) for Windows 10

That sounds like a fair interpretation. :slight_smile:

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

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???

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!