Lesson 3 of 8 · 12 min

Challenge: The kombi conductor's fare calculator

Kuda is a conductor on a kombi from Harare to Chitungwiza. Families climb on together and ask, "How much for all of us?" He wants his phone to answer instantly.

The fare is $1, but not everyone pays the same.

Your task

Write kombiFare(ages). It takes a list of the passengers' ages and returns the total the group pays, in dollars.

Passenger Age Pays
Baby under 3 free
Child 3 to 11 half fare, $0.50
Adult 12 to 59 full fare, $1
Pensioner 60 and over half fare, $0.50

Group discount: if 6 or more passengers in the group pay something, the whole group gets $1 off the total. Babies ride free, so they do not count towards the 6.

kombiFare([30, 41, 18])                 // 3
kombiFare([35, 33, 7, 1])               // 2.5   (two adults, one child, a baby)
kombiFare([20, 20, 20, 20, 20, 20])     // 5     (6 adults: 6 - 1)
kombiFare([20, 20, 20, 20, 20, 1])      // 5     (only 5 pay, no discount)
kombiFare([])                           // 0

Rules

  • Ages are whole numbers, 0 or more.
  • Return a number, not text: 2.5, not "$2.50".
  • An empty list means nobody is travelling: return 0.

Tips

  • Read the table from top to bottom as an if / else if / else chain. Watch the edges: is a 3-year-old free or half price?
  • Keep two variables as you loop: the total so far, and how many passengers have paid.
  • Apply the discount once, after the loop, not inside it.
main.js
function kombiFare(ages) {
  // TODO: add up the fare for each passenger, then apply the group discount
  return 0;
}

console.log(kombiFare([35, 33, 7, 1]));  // should print 2.5

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…