What is wrong in this code. (runnerAge>18 && registeredEarly) ? (raceNumber+=1000) : /*Do Nothing */;

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

let registeredEarly = true;

let runnerAge = 10;

(runnerAge>18 && registeredEarly) ? (raceNumber+=1000) : /*Do Nothing */;

Can someone help me to find out an error in the above line, looks ok to me?

Hello @rajivprasad623192626, welcome to the forums! Ternary expression’s else (:) must do something, otherwise it will throw an error. In you case, raceNumber will have 1000 added to it if the condition is true, but it doesn’t do anything if the condition isn’t true.

thanks for your remark. In that case, it is better to use an if-condition

1 Like

Hi everyone, I’m fairly new to this and have a similar problem with the same challenge using the if statement. The section specifically states to use an ‘if’ statement to check if the runner is over 18 and also whether the runner registered early. If so, to then add 1000 to the race number. But…

let raceNumber = Math.floor(Math.random() * 1000);
let earlyRegister = true;
let runAge = 19;

if (runAge >= 18 && earlyRegister = true) {
raceNumber += 1000
}

…I get the following response:

if (runAge >= 18 && earlyRegister = true) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ReferenceError: Invalid left-hand side in assignment

I have no idea what I need to do and wondered if someone can guide me some way through it. Much appreciated.

Take a look at this part of the code:

You’re using =, which is the assignment operator. What operator should you be using?

1 Like

Hi @codeneutrino. You’re the best. I did use the assignment operator rather than the comparison operator. The error from the system threw me as I understood the error to point to the left hand-side of the condition rather than the right-hand side where the issue was. Thank you for the assist. I can carry on with learning now :slight_smile:

1 Like