Hi all, apologies for posting again, but I am still struggling with Exercise 24 in the lesson “Practice Exercise - Booleans & If, Else statements”.
I have approached the task like this:
var capacity = 30;
var station1 = prompt(“Station 1! How many people go on the bus?”)
if (capacity < station1) {
console.log(“The bus is full”, +(station1-capacity) + “have to walk”); }
else {
console.log(station1); }
14
This has worked so far, producing 14 as expected, however, when I add station 2 to this, I get the answer “The bus is full 1383 have to walk” instead of the expected “13”, that I entered in the prompt:
var station2 = prompt(“Station 2! How many people go on the bus?”);
if (capacity < (station1 + station2)) { console.log(“The bus is full”, +((station1 + station2)-capacity) + “have to walk”); }
else {
console.log(station2); }
My “plan” was to receive 13 as the output, and add station3 in the same manner I added station2 in the next step. I had a brief look at the solution, which was approached in a different manner, but I believe my approach can still work. Can anyone point out what error I made? I recognised hat the issue arises when station1 and station2 are added up in the parentheses, but it isn’t quite clear to me why the correct addition doesn’t occur. Thank you very much for any help with this!