There is an error in the conditional of my code and I can't find why

I am currently doing the Race Day project for conditionals on the Javascript course. I’ve tried running my code but it tells me there is an error with one of my conditions.

let raceNumber = Math.floor(Math.random() * 1000);
let early = true;
let runnerAge = 15;

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

if (runnerAge > 18 && early = true) {
  console.log(`Runner number ${raceNumber}, your race will commence at 9:30 am.`)
} else if (runnerAge > 18 && early = !true) {
   console.log(`Runner number ${raceNumber}, your race will commence at 11:00 am.`)
} else if (runnerAge < 18) {
  console.log(`Runner number ${raceNumber}, your race will commence at 12:30pm.`)
}

This is what appears in the console:

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

Here’s a link to the project

I don’t understand what it wrong with the condition it is pointing to. I’m completely new to JS so any help would be appreciated!

Hey there!

Double check what you’re using for checking if early is true, for comparisons you cannot use just one =, those are for assigning values, comparisons use == or === depending on if the type is also being checked (the latter is “strict” and requires the two to be of the same type, the former does not).

As an aside, since you’re checking if a boolean variable is true or not, you actually don’t need a comparison at all and can just do: && early.

Happy coding! :slight_smile:

Oh yeah I completely forgot about using == and ===. Thank you!

1 Like

True you still get en error with just && early

You sure about that? Works fine for me in testing an example, mind sharing the code you tried so I can compare?
image

It worked for me aswell

1 Like

Mabey Replit broke

let raceNumber = Math.floor(Math.random() * 1000);
let early = true;
let runnerAge = 15;

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

if (runnerAge > 18 && early) {
  console.log(`Runner number ${raceNumber}, your race will commence at 9:30 am.`)
} else if (runnerAge > 18 && early = !true) {
   console.log(`Runner number ${raceNumber}, your race will commence at 11:00 am.`)
} else if (runnerAge < 18) {
  console.log(`Runner number ${raceNumber}, your race will commence at 12:30pm.`)
}
/home/runner/UnhappyRoyalblueMosaic/index.js:11
} else if (runnerAge > 18 && early = !true) {
           ^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid left-hand side in assignment```

You’re only using a single equals statement, hence the assignment error, on the second conditional this time. The error is unrelated to the first conditional and using && early :slight_smile:

1 Like

Well now I know I am officially blind

1 Like