Drill

ProblemsJavaScript › pricing

Discount, but never below the floor

easypricingJavaScript

Sales can discount freely, except that a contract sets a price the item may never go under.

discountWithFloor(price: int, percent: int, floorPrice: int) → int

Solve it in the editor →

Where you start

function discountWithFloor(price, percent, floorPrice) {
  
}

Worked examples

CallResult
discountWithFloor(1000, 20, 500)800
discountWithFloor(1000, 70, 500)500
discountWithFloor(1000, 20, 1200)1200
discountWithFloor(1000, 0, 500)1000

Hint

Two steps, in order: discount, then clamp.

Reference solution in JavaScript
function discountWithFloor(price, percent, floorPrice) {
  const cut = percent < 1 || percent > 100 ? price : price - Math.floor((price * percent) / 100);
  return Math.max(cut, floorPrice);
}

The same problem in another language

More pricing problems in JavaScript