Race Day [Exercise]

  • This exercise is about an annual race which has a lot of participants and each participant will be given instructions based on their registration.

  • There are adult runners above 18 years old and youth runners below 18 years of age.

  • Runners are assigned a race number and start time based on their age registration.

Race number:

  • Early adults recieve a race number at or 1000 above

  • All others recieve a race number below 1000

Start time:

  • Adult registrants run at 9:30 am or 11:00 am.

    • Early adults run at 9:30 am.

    • Late adults run at 11:00 am.

  • Youth registrants run at 12:30 pm (regardless of registration).

Solution:

let raceNumber = Math.floor(Math.random() * 1000);

let runnerRegisteredEarly = false;

let runnerAge = 18;

if (runnerAge >= 18 && runnerRegisteredEarly === true) {
  raceNumber + 1000;
}

if (runnerAge > 18 && runnerRegisteredEarly === true) {
  console.log(`${raceNumber} will race at 9.30am.`);
} else if (runnerAge > 18 && runnerRegisteredEarly === false) {
  console.log(`${raceNumber} will race at 11:00am.`);
} else if (runnerAge < 18){
  console.log(`Youth registrants run at 12:30 pm (regardless) of registration`);
} else {
  console.log('Please see the runners desk.')
}

// Output: Please see the runners desk.

Solution with switch and case statement:

et raceNumber = Math.floor(Math.random() * 1000);

let runnerRegisteredEarly = false;

let runnerAge = 18;

switch (true) {
  case (runnerAge > 18 && runnerRegisteredEarly === true):
  raceNumber + 1000;
}

switch (true) {
  case (runnerAge > 18 && runnerRegisteredEarly === true):
  console.log(`${raceNumber} will race at 9.30am.`);
  break;
  case (runnerAge > 18 && runnerRegisteredEarly === false):
  console.log(`${raceNumber} will race at 11:00am.`);
  break;
  case (runnerAge < 18):
  console.log(`Youth registrants run at 12:30 pm (regardless) of registration`);
  break;
  default :
  console.log('Please see the runners desk.');
  break;
}

// Output: Please see the runners desk.

Last updated