0

Stuck on a javascript switch assignment. I'm still fairly new to this stuff but can't seem to find anything in my course books or online to get this program to work. The task was to switch if statements to switch code. Any help or suggestions would be greatly appreciated.

var myAge = prompt("Enter your age:", "30");

switch (myAge) {

  case myAge! >= 0:
  case myAge <= 10:
    document.write("myAge is NOT between 0 and 10 <br />");

  case myAge >= 0:
  case myAge <= 10:
    document.write("myAge is between 0 and 10 ");
    break;

  case myAge >= 80:
  case mmyAge <= 10:
    document.write("myAge is 80 or above or 10 or below ");
    break;

  case myAge >= 30:
  case myAge <= 39:
    document.write("myAge is between 30 and 39 ");
    break;

  case myAge >= 80:
  case myAge <= 89:
    document.write("myAge is between 80 and 89 ");
    break;
}
Sagar V
  • 12,158
  • 7
  • 41
  • 68
cameragrl37
  • 1
  • 1
  • 3
  • 1
    you're missing a `break;` in the first case – Alejandro Garcia Anglada Jun 16 '17 at 13:53
  • 1
    You would need `switch (true) {` to match your boolean case expressions and `!>` is not a valid operator. – Alex K. Jun 16 '17 at 13:55
  • @Community ... sorry but this is clearly a different question...let's put it in this way: what do You think cameragrl37 expects from a 87 input?... I would say he expects 3 msgs... this can't be written in one switch! – fedeghe Jun 19 '17 at 15:21

4 Answers4

0

Make sure you are using an int instead of a string just in case do:

var myAge = parseInt(prompt("Enter your age:", "30"));

And make sure you add break; statements to all the cases.

Remove the ! character in the first case: myAge! to myAge

What exact error are you getting?

0

On the first "case myAge" the comparison operator does not work. If you are trying to say that it is not equal to, you should put !=, not !>=.

0

I thought that could work...but

-------------------------OLD ANSWER

try the following; btw are You sure You need the document.write ?

var myAge = ~~prompt("Enter your age:", "30"),
    msg = [];
switch (true) {

    case myAge < 0 && myAge > 10:
        msg.push("myAge is NOT between 0 and 10");

    case myAge >= 0 && myAge <= 10:
        msg.push("myAge is between 0 and 10");

    case myAge >= 80 || myAge <= 10:
        msg.push("myAge is 80 or above or 10 or below");

    case myAge >= 30 && myAge <= 39:
        msg.push("myAge is between 30 and 39");

    case myAge >= 80 && myAge <= 89:
        msg.push("myAge is between 80 and 89");
}
document.write(msg.join('<br />'));

but is not, here's why: first is pretty clear that You want to get more that one validation message; unluckily this cannot be written in a single switch cause to run more than one you should remove break; but this means that the first one satisfied causes all the followinfg ones to be executed regardless the condition...

said that you have to move to a "multi-if" that could look like the following:

var myAge = parseInt(prompt("Enter your age:", "30"), 10),
    msg = [];

if (myAge < 0 && myAge > 10)
    msg.push("myAge is NOT between 0 and 10");
if (myAge >= 0 && myAge <= 10)
    msg.push("myAge is between 0 and 10");
if (myAge >= 80 || myAge <= 10)
    msg.push("myAge is 80 or above or 10 or below");
if (myAge >= 30 && myAge <= 39)
    msg.push("myAge is between 30 and 39");
if (myAge >= 80 && myAge <= 89)
    msg.push("myAge is between 80 and 89");

console.log(msg.join("\n"));
fedeghe
  • 1,243
  • 13
  • 22
0

You were missing break and typo mmyAge. and pass true to switch.

var myAge = parseInt(prompt("Enter your age:", "30"));
age(myAge);
function age(myAge){

    switch (true) {
        
        case (myAge >= 0 && myAge <=10):
            document.write("myAge is NOT between 0 and 10 <br />");
        break;
        case myAge >= 0 && myAge <= 10:
            document.write("myAge is between 0 and 10 ");
            break;

        case myAge >=80 && myAge <=10:
            document.write("myAge is 80 or above or 10 or below ");
            break;

        case myAge >= 30 && myAge <=39:
            document.write("myAge is between 30 and 39 ");              
            break;

        case myAge >= 80 && myAge <= 89:
            document.write("myAge is between 80 and 89 ");
            break;
        default:
              console.log(myAge);
        }
        
  }
Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40