Drill

ProblemsJavaScript › billing

Clamp a day into a billing term

easybillingJavaScript

A billing window runs from day 1 to day termDays. Clamp any given day to within that window.

clampToTerm(termDays: int, day: int) → int

Solve it in the editor →

Where you start

function clampToTerm(termDays, day) {
  
}

Worked examples

CallResult
clampToTerm(31, 35)31
clampToTerm(31, 0)1
clampToTerm(31, 20)20
clampToTerm(0, 5)0

Hint

A pair of comparisons: too low pulls up, too high pulls down.

Reference solution in JavaScript
function clampToTerm(termDays, day) {
  if (termDays <= 0) return 0;
  if (day < 1) return 1;
  if (day > termDays) return termDays;
  return day;
}

The same problem in another language

More billing problems in JavaScript