Why doesn't this work (Race day)

let raceNumber = Math.floor(Math.random() * 1000);
let early = true
let age = 19

if (age >= 18 && early = true){
  raceNumber+=1000; 
  console.log(raceNumber)
}

Why can’t I do the above? For some reason it only works if I do them individually


let raceNumber = Math.floor(Math.random() * 1000);
let early = true
let age = 19

if (age >= 18){
  raceNumber+=1000; 
  console.log(raceNumber)
}
let raceNumber = Math.floor(Math.random() * 1000);
let early = true
let age = 19

if (early = true){
  raceNumber+=1000; 
  console.log(raceNumber)
}

(https://www.codecademy.com/courses/introduction-to-javascript/projects/race-day)

That reason is that you mix up assignment (=) with comparison (== or ===).
Your second code snippet also does not what you think it does:

let early = false

if (early = true){
  console.log(early) // logs true
}
1 Like