There was a task with runners schedule in JS introduction, can someone help to fix a code?

Hello there was a task to make a schedule for runners depending if they are above below or equal 18 years and if they pre-registered
old pre-registered need to be announced at 9:30
old not pre-registered at 11:00
young at 12:30
18 years old need to check at the front desk
Here I have two attempts to make it through switch keyword but there is no result appearing as no mistakes also so I’m confused (P.S. I made exercise how it must be done, just curious)
/switch (registeredEarly) {
case (true):
switch (runnersAge) {
case (runnersAge > 18):
raceNumber += 1000;
console.log(Participant number ${raceNumber} start at 9:30 am);
break;
case (runnersAge === 18):
console.log (Participant number ${raceNumber} for further information come to registration desk);
break;
case (runnersAge < 18):
console.log(Participant number ${raceNumber} start at 12:30 pm);
break;
}
case (false):
switch (runnersAge) {
case (runnersAge > 18):
console.log(Participant number ${raceNumber} start at 11:00 am);
break;
case (runnersAge === 18):
console.log(Participant number ${raceNumber} for further information come to registration desk);
break;
case (runnersAge < 18):
console.log(Participant number ${raceNumber} start at 12:30 pm);
}
}
/

/switch (registeredEarly, runnersAge) {
case (true && runnersAge > 18):
raceNumber += 1000;
console.log(Participant number ${raceNumber} start at 9:30 am);
break;
case (true || runnersAge > 18):
console.log(Participant number ${raceNumber} start at 11:00 am);
break;
case runnersAge < 18:
console.log(Participant number ${raceNumber} start at 12:30 pm);
break;
case runnersAge === 18:
console.log(Participant number ${raceNumber} for further information come to registration desk);
break;
}
/

/*If (registeredEarly && runnerAge > 18)
{
raceNumber+=1000;
console.log(Participant number ${raceNumber} start at 9:30 am);
}
else if(registeredEarly || runnerAge>18){
console.log(Participant number ${raceNumber} start at 11:00 am);
} else if(runnerAge< 18){
console.log(Participant number ${raceNumber} start at 12:30 pm);
} else {
console.log(Participant number ${raceNumber} for further information come to registration desk);
}
*/

Can you format this as code in the post editor using three backticks above and below the code block?
This will make it easier for us to parse your code and help you.
You can also use the code button which looks like : </>

I will note that you cannot use logical expressions in the way you’ve done so in switch/case statements. At least in part. You see, you can put switch (registeredEarly) and later follow up with case (true), but you can’t use switch (runnersAge) and follow up with case (runnersAge > 18). Whatever is inside the switch parentheses is evaluated, and if there is a case which it evaluates to, then it runs the code inside that case block.
For example:

let runnersAge  = 19
switch (runnersAge > 18) {
  case (true):
    console.log('Runner is older than 18')
  case (false):
    console.log('Runner is not older than 18')
}

// this switch would log 'Runner is older than 18' because runnersAge *is* greater than 18

So, for such things, I’d recommend using an if-else if-else block.

sorry, didn’t know that.

So basically saying we can’t use logical expressions in case parenthesis?

Not in that way. You can technically have expressions inside, but they don’t work the same way if’s do.

You can, but you would need to express it like this

switch (true) {
  case (conditional 1):...
  case (conditional 2):...
  ...
}

switch (a) { case b... is basically the same as if (a == b)...

1 Like

So you mean that instead of putting variable of comparing in switch parentheses I need to put there true boolean?

Yes. Think about what you’re doing when you write a switch/case block.

Switch (runnersAge) {
  case 10:
    return true;
}

is equivalent to

if (runnersAge == 10) {
  return true;
}

when you make your case expression a conditional instead, like

switch (runnersage) {
  case (runnersAge > 10) {

it would evaluate similarly to

if (runnersAge == (runnersAge > 10))

which when resolved is either

if (10 == true)

or

if (10 == false)

10 does not equal true or false, so this always fails. Instead you can use switch (true) so that your case statement is only executed when (true == true). It’s a bit odd looking, but enables you to use switch in this context.

Thank you very much for explanation I will need to work a bit more with conditionals.

One small correction: because you used == instead of ===, the first equality will return true (i.e. 10 == true). The loose equality operator causes type coercion, and since 10 is truthy, 10 and true are loosely equal.

1 Like

Nice catch, thank you!