Great to see you are going through
Now the NaN issue appears from a bad use of an operator in some variables, I have set some comments in your code, made some improvements so you can understand better how to fix some issues you had with the console.log() when your bus is full.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
</head>
<body>
<script>
//amount of passengers always must start at 0
var passenger = 0
//parse to int
let station1 = parseInt(prompt("How many people are coming on the bus?"))
//Example: station1 = 31
if (station1 + passenger >= 30){ // 0 + 31 is >= 30
passenger + station1 // ??? this should be assigned to a variable, maybe "passenger += station1" ??
//operations on variables must be used inside () to calculate properly
/* also can use another variable to calculate that
let total_passenger = (passenger + station1 - 30)
then just use "total_passenger" variable into the next console, result should be the same
*/
console.log("The bus is full"+" "+(passenger + station1 - 30)+" "+"have to walk.")
passenger = 0 //passenger should be reset to 0 so the next IF start with new values
//total_passenger = 0
//also reset "total_passenger" in case you want to use it
}
else {
passenger += station1;
console.log(station1 + " passengers are going on the bus.")
}
let station2 = parseInt(prompt("How many people are coming on the bus?"))
if (station2 + passenger >= 30) {
console.log("The bus is full"+" "+(passenger + station2 - 30)+" "+"have to walk.")
passenger = 30
}
else {
passenger += station2;
console.log(station2 + " passengers are going on the bus.")
}
let station3 = parseInt(prompt("How many people are coming on the bus?"))
if (station3 + passenger >= 30) {
console.log("The bus is full"+" "+(passenger + station3 - 30)+" "+"have to walk.")
passenger = 30
}
else {
passenger += station3;
console.log(station3 + " passengers are going on the bus.")
}
</script>
</body>
</html>
Most of the time is some kind of miss type on a code line, it could be an extra symbol, example: an extra ; [] {} / ()
, Unexpected token
.
Some times bad invoke on a variable, like trying to bind a integer value into an string variable (Unexpected number
)
Basically a syntax error, meaning something in your syntax is not correct, what i always do is to verify closely the codeline that the error point to me.
If you have any more questions, please let us know so we can help you!
Carlos Z.