Drill

ProblemsJavaScript › logistics

Parcel discount band

mediumlogisticsJavaScript

A monthly contract discounts each parcel once the month's total reaches a threshold: every parcel from then on ships 15% cheaper.

bulkDiscountBand(parcelCosts: list<int>, threshold: int) → int

Solve it in the editor →

Where you start

function bulkDiscountBand(parcelCosts, threshold) {
  
}

Worked examples

CallResult
bulkDiscountBand([100,100,100,100], 250)370
bulkDiscountBand([100,100], 200)185
bulkDiscountBand([100,100,100], 1000)300
bulkDiscountBand([], 0)0

Hint

Walk the values; once the running total crosses the threshold everything from that parcel on is discounted.

Reference solution in JavaScript
function bulkDiscountBand(parcelCosts, threshold) {
  let total = 0, running = 0;
  for (const cost of parcelCosts) {
    running += cost;
    total += running >= threshold ? Math.floor((cost * 85) / 100) : cost;
  }
  return total;
}

The same problem in another language

More logistics problems in JavaScript