Drill

ProblemsJavaScript › orders

Work out the shipping charge

mediumordersJavaScript

The carrier bills in weight bands, and anything over five kilos is charged per extra kilo started.

shippingCost(grams: int, express: bool) → int

Solve it in the editor →

Where you start

function shippingCost(grams, express) {
  
}

Worked examples

CallResult
shippingCost(400, false)3000
shippingCost(500, false)3000
shippingCost(1500, false)5000
shippingCost(5000, false)8000

Hint

For the top band, round the excess up to whole kilos: (excess + 999) / 1000 in integer arithmetic.

Reference solution in JavaScript
function shippingCost(grams, express) {
  if (grams <= 0) return 0;
  let cost;
  if (grams <= 500) cost = 3000;
  else if (grams <= 2000) cost = 5000;
  else if (grams <= 5000) cost = 8000;
  else cost = 8000 + Math.ceil((grams - 5000) / 1000) * 1500;
  return express ? cost * 2 : cost;
}

The same problem in another language

More orders problems in JavaScript