Drill

ProblemsJavaScript › orders

Is this status change allowed

mediumordersJavaScript

Orders move through a fixed lifecycle, and a support tool needs to know whether a requested change is one the system permits.

canTransition(current: string, target: string) → bool

Solve it in the editor →

Where you start

function canTransition(current, target) {
  
}

Worked examples

CallResult
canTransition("placed", "paid")true
canTransition("paid", "refunded")true
canTransition("shipped", "delivered")true
canTransition("placed", "shipped")false

Hint

A set of allowed pairs is easier to get right than a nest of ifs.

Reference solution in JavaScript
function canTransition(current, target) {
  const allowed = ['placed>paid', 'placed>cancelled', 'paid>packed', 'paid>refunded', 'packed>shipped', 'shipped>delivered'];
  return allowed.includes(current + '>' + target);
}

The same problem in another language

More orders problems in JavaScript