Problem:
I’m having a bit of issues understanding how to complete this task and will appreciate a bit of guidance where i’m going wrong:
Northdrivers Taxi Company™️ have asked for your help writing a function which will calculate the cost of getting to the party! Journeys are priced as follows:
- Journeys up to 3 minutes long have a flat base rate cost of £5
- Every minute after the first 3 will cost an extra 70p
- The length of the journey is always rounded up to a whole number of minutes
The calculateTaxiFare function should take a number representing the length of a taxi journey in seconds, and return a number representing the cost of that journey in pence.
my code:
function calculateTaxiFare(seconds) {
// Your code goes here...
const flatFare = 500
const minutes = Math.ceil(seconds / 60)
console.log(minutes)
if(minutes<=3){
return(flatFare)
}
if(minutes>3){
let newMin = minutes-3;
return (flatFare + (newMin * 70))
}
}
The errors i’m getting:
Returns total fare when called with a value that does not equate to a
whole number of minutes
✕ AssertionError: expected 920 to equal 990
Returns total fare when called with a value that does not equate to a
whole number of minutes. Minutes must always be rounded up
✕ AssertionError: expected 500 to equal 570
Thank You
Solution:
- To round up to the nearest whole integer, use Math.ceil()
- Unless you want every hour to reset the price, I have no idea what
seconds % 3600
is meant to be for - Math.round() is useless on integers
- You can calculate the extras multiplier by finding the
minutes - 3
or0
if it’s less than three minutes
const FLAT_FARE = 500;
const EXTRAS_MULTIPLIER = 70;
const EXTRAS_THRESHOLD = 3;
function calculateTaxiFare(seconds) {
const minutes = Math.ceil(seconds / 60); // round up
return (
FLAT_FARE + Math.max(minutes - EXTRAS_THRESHOLD, 0) * EXTRAS_MULTIPLIER
);
}
console.log("545s =", `${calculateTaxiFare(545)}P`);
console.log("220s =", `${calculateTaxiFare(220)}P`);