Switch


#1

i dont know why the codes below does not run. Can someone help to debug possible errors:

var grade = prompt(“What grade is it ?”);
switch (grade) {
case “Diesel”:
alert("It sells for $3.00 ");
break;
case “Fuel”:
alert(“It sells for $4.00”);
break;
case “premuim”:
alert(“It sells for $5.00”);
break;
default:
alert(“Invalid entry”);
break;
}


#2

Can you write a link to the lesson that you were stuck on :stuck_out_tongue_winking_eye:


#3

Hi @xlong45,

I’ve tested it, your code do work. I think you’ve given the wrong input thus you’d think it doesn’t work.

Is this a code you write for your own project OR for an exercise?


If there’s an exercise, please provide:

  • A link to your exercise (we need to see which exercise you’re talking about, some exercises have many steps/stages)
  • Tell us what problem you faced or what error you had.
  • Paste and format your code accordingly read more here ( We need to see your code so we can point out where is the problem)

If this code is for your own project, I would recommend using .toLowerCase() method for your prompt. It changes every input into all lower case string.

var grade = prompt(“What grade is it ?”).toLowerCase();

Then change all your switch cases into all lower case “Diesel” into “diesel” , “Fuel” into “fuel” and so on.

I can’t post the full edited code for you, you have to learn to work it out, try make the changes yourself, test it if it works.

Post the code again with the format if you have any trouble.


Hope this helps. Cheers :slight_smile:


#4

Many thanks. The code is for my own project . I will format it again as you suggested and post the new version.


#5

Cool @xlong45,

Can’t wait to see your working code. :wink: