Drill

ProblemsJavaScript › logistics

Cost of a planned route

easylogisticsJavaScript

A route is a list of legs; each leg has a distance and a per-kilometre rate. Sum the cost of the whole route.

routeCost(legs: list<Leg>) → int

Solve it in the editor →

Where you start

function routeCost(legs) {
  
}

Worked examples

CallResult
routeCost([{"distance":10,"rate":5},{"distance":4,"rate":8}])82
routeCost([{"distance":1,"rate":1000}])1000
routeCost([])0
routeCost([{"distance":0,"rate":500}])0

Hint

Multiply per leg and add.

Reference solution in JavaScript
function routeCost(legs) {
  let total = 0;
  for (const leg of legs) total += leg.distance * leg.rate;
  return total;
}

The same problem in another language

More logistics problems in JavaScript