Javascript for the Taxi Fare Calculator: Explained

The HEAD Section Explained
I start out by declaring the function "fare." I include two if statements in the function.

The first if statement say that if it's rush hour (the value of rush is entered by the user) that the delay would be an average of five minutes per block. If it's not rush hour, the delay would be one fifth of the number of blocks. (example: If you need to go ten blocks, and it isn't rush hour, you'd spend approximately two minutes idle.)

The second if statement depends on the time. If it's during evening hours (8 PM- 6 AM),the variable time = yes, and so the inital fee before you move an inch is $2.50. If not, then the initial fee is $2.00

Next we define the total by evaluating the string: fee+blocks*0.075+delay*0.2.
You're charged 7.5 cents for every block you travel, and 20 cents for every minute spent stationary.

Now here's where it gets tricky: we want to format this in money. So I created a new variable, called "cost," I defined it as Math.round(total*100)/100 . . . which rounds the total*100 to an integer and then divides it by 100, so we have at the most two numbers after the decimal point.

Now for some ifs . . .
If the cost rounded up to the next whole number (thanks to Math.ceil) is equal to the cost without doing anything, then we write: The approximate cab fare is: $" + (cost) + ".00".

(Ex. If the cost=30, and the cost rounded to the next whole number is the same (since it's not fractional) Therefore: it's a whole dollar amount so we can just stick on the decimal and two zeros.)

Now, if the cost*10 is equal to the cost*10 rounded to the next whole number, we print the cost as a float and add the second zero.

(Ex. If the cost=4.5, then the cost*10= 45; the cost*10 rounded to the next whole number is still 45, so we know it's a dollar amount with a number in the tenths place and a zero in the hundreds place. We then print the float 4.5 and stick on the second zero.)

Now for else:
if the cost doesn't meet either of the other two conditions, it means we have numbers other than 0 in the two decimal places, so we can just print it as a float.

Then I calculated the tip by rounding 15% of the cost to the nearest whole number, and I tacked on the decimal and two zeros. I figured I'd round to the nearest whole number because it's easier to pay a tip in whole dollars.

The BODY Section Explained
I declare a variable blocks, and prompt the user for the amount. I left it as a float because people could have fractional answers, even though it's not as likely. This variable goes into our function, which was discussed above. For every block you travel you're charged 7.5 cents.

Then I asked the user a yes or no question. If the answer is yes, the user intends to ride during evening hours and will be charged an initial fee of $2.50 instead of $2.00.

I ask the user a final yes or no question, whether or not it's rush hour. The answer goes into an if statement to determine how much of a delay the rider must endure, and pay for (on average).

Finally, I call the function "fare", which is discussed above.