Race Day - using Switch Cases

Hello - I need help figuring out why my code will not work I want it to. I am trying to get it so that this will produce the same result as an if, else if, else tree. So far, it doesn’t take into account whether early registration is true or false. Please help. Thanks.

let raceNumber = Math.floor(Math.random() * 1000);
let runnerAge = 22
let earlyRegistration = false
let adultOrChild = ''

switch(runnerAge && earlyRegistration) {
	case (earlyRegistration && runnerAge > 18):
		adultOrChild = 'Adult'
		raceNumber += 1000
		console.log(`Runner number ${raceNumber}, you will be running at 9:30AM, in the ${adultOrChild} class.`)
  break
	case (!earlyRegistration && runnerAge > 18):
		adultOrChild = 'Adult'
		raceNumber += 1000
		console.log(`Runner number ${raceNumber}, you will be running at 11:00AM, in the ${adultOrChild} class.`)
  break
	case runnerAge < 18:
		adultOrChild = 'Child'
		console.log(`Runner number ${raceNumber}, you will be running at 12:30PM in the ${adultOrChild} class.`)
  break
	case runnerAge === 18:
		console.log(`Runner number ${raceNumber}, please see the front desk for more info.`)
  break
	default:
		console.log('Are you here to sign up for a race?')
}

Hi @ginobjj
Welcome to the forum!
Why don’t you want to use if else? What you did is a typical case for that.
Switch takes a single variable and compares it to its value, like

let age = 18;
switch(age) {
  case 18 : 
  // do something
}

You cannot use an if clause as you did in a switch statement.

I did it correctly with if else statements but I wanted to practice the switch statements a bit also. So I did both, but this one did not work as I expected.


Gino

And hello :wink:

Thank you!


Gino
1 Like

Ah, ok. You’re welcome.
Then leave it at that. In this exercise, it doesn’t really make sense to use switch statements. There’ll be others…

1 Like

Note:
You could use switch…case blocks in this example (although I wouldn’t recommend it):

switch (true){
  case someExpression:
    //code
}
1 Like

Hey @codeneutrino
I use the switch with true a lot actually (when the conditional cases are 3 or more). There’s something wrong with it?

Hey ginobjj
if you want somewhere on codeacademy where to practise conditionals I’d suggest this code challenge exercise: Javascript Fundamentals - Code Challenge

If you want to practise only the switch statement you can do it for almost all of them (if not all).

In some cases you could use a switch using as conditional the value true, as suggested by codeneutrino.
Then you write your cases in a way that they return a bolean. E.g.:

switch(true){
  case (1>2):
    console.log("I got less, but it's worth way more");
    break;
  case (1===2):
    console.log("I got half, but it's worth double");
    break;
  case (1<2):
    console.log("Well, this was expected");
    break;
  default:
    console.log("How?!");
    break;
}

There’s nothing inherently wrong with it, I just don’t think it’s very good coding practise; switch is just meant for looking at variable’s values, not expressions…

1 Like

You may find this interesting :slight_smile:

2 Likes

Interesting indeed, especially regarding what it says about the performance/speed of evaluation. Yet I don’t think that it contradicts what @codeneutrino says regarding use cases as in the examples given, they’re passing a variable to switch and use a single value as the case.

2 Likes