Skip to content

Instantly share code, notes, and snippets.

@gojun077
Created September 11, 2012 14:02
Show Gist options
  • Save gojun077/3698720 to your computer and use it in GitHub Desktop.
Save gojun077/3698720 to your computer and use it in GitHub Desktop.
CodeAcademy - Hello New York, Sec 1/1, Ex 4/5
/*
4. After Dark
The cost of a Taxi ride varies by time of day (we pay
drivers extra to work the night shifts). A flat surcharge
of $0.50 is added to cost for any trip starting at 8pm
and continues until we hit 6am.
Expand your taxiFare function to accept a second parameter
called hourOfDay which represents the time of pickup. Add
a night surcharge of $0.50 during the appropriate hours,
specified as a number between 0-23.
Steps that may help:
a) Add the second parameter into the taxiFare function.
b) Starting on line 11, add an if / else statement. We want
the nightSurcharge to be added for the time starting
(and including) 8pm, and ending at (but NOT including) 6am!
How do we represent or? We use ||
*/
// add a parameter called hourOfDay to the function
var taxiFare = function (milesTraveled, hourOfDay) {
var baseFare = 2.50;
var costPerMile = 2.00;
var nightSurcharge = 0.50; // 8pm to 6am, every night
var cost = baseFare + (costPerMile * milesTraveled);
// add the nightSurcharge to the cost starting at
// 8pm (20) or if it is before 6am (6)
if (hourOfDay < 6 || hourOfDay >= 20) {
return cost + nightSurcharge;
}
else
return cost;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment