By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Javascript Taxi Fare Calculator
JavaScript

Javascript – Javascript Taxi Fare Calculator

Admin
Last updated: 2023/12/19 at 5:17 PM
Admin
Share
2 Min Read
Javascript Taxi Fare Calculator

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:

Contents
Problem:Solution:

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:

  1. To round up to the nearest whole integer, use Math.ceil()
  2. Unless you want every hour to reset the price, I have no idea what seconds % 3600 is meant to be for
  3. Math.round() is useless on integers
  4. You can calculate the extras multiplier by finding the minutes - 3 or 0 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`);

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Not sure where to put tags When migrating to Next 13 app dir from Next12 pages dir Javascript – Not sure where to put tags When migrating to Next 13 app dir from Next12 pages dir
Next Article how i can make the result show when click on select without button and display on text box Javascript – how i can make the result show when click on select without button and display on text box
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?