I don't know what's wrong with my code (race day project)

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

var registeredRunner = true;

var runnerNumber = 50;



if (registeredRunner = true || runnerNumber > 18) {
  raceNumber += 1000 ;
}


if (runnerNumber > 18) {
  console.log(`Runner number ${raceNumber} will race at 9:30.`);
} else if (runnerNumber > 18 && registeredRunner != true ) {
  console.log(`Runner number ${raceNumber} will race at 12:30.`);
}

I can’t get the second case values even when I change registeredNumber to false :confused:

Hello @wall1id. Before explaining what is wrong, you might want to take a look at these:

Specifically, the equal signs. What do equality operators look like in JS? Are they =, or is that something else?

Now, for the logic:

Here, you’re basically saying: ‘if the runner is over 18, do something.’ In the else if, you’re saying: ‘if the runner is over 18 and something else, execute some code.’ But that can never run, because the first condition runnerNumber > 18 as already been checked by the above statement. So if that statement does not execute, then neither will the else if, as runnerNumber would not be above 18.
I hope this helps!

1 Like

Thanks! now can see how illogical it is. I forgot to add another condition to the if statement:

if (runnerNumber > 18 && registeredRunner === true) {
  console.log(`Runner number ${raceNumber} will race at 9:30.`);
} else if (runnerNumber > 18 && registeredRunner === false ) {
  console.log(`Runner number ${raceNumber} will race at 12:30.`);
}
1 Like

Hello @wall1id. Your logic is not flawed, but there is an easier way to do that:

if (object1 > condition1 && condition2) {
//code
} 

In the example above, we check if object1 is greater than the condition. But since we are only checking to see if condition2 is true or false, we only have to write it; we don’t have to go condition2 === true. In your case, that could be applied here:

if (runnerNumber > 18 && registeredRunner) {
 console.log("abc");
}

Here you check if runnerNumber is greater than 18, and you check if registeredRunner is true.
Happy coding!

1 Like