MediumJavaScriptPractice mode: untimed, 3 hints
Order lines from a form
orderLines(items) builds order lines from a form. A missing quantity (absent or null) means 1. An explicit 0 means the customer removed the item, so that line is left out.
The code has exactly one planted bug. Reproduce it with the tests, fix it with the smallest change, then submit.
Visible tests
removed item stays removed
- orderLines([{"sku":"A"},{"sku":"B","qty":0}]) → [{"sku":"A","qty":1}]
given quantity
- orderLines([{"sku":"C","qty":3}]) → [{"sku":"C","qty":3}]
Hints 0/3
// Builds order lines from a form. A missing quantity means 1; an explicit 0 means the
// customer removed the item, so that line is left out.
function orderLines(items) {
const lines = [];
for (const item of items) {
const qty = item.qty || 1;
if (qty > 0) lines.push({ sku: item.sku, qty });
}
return lines;
}
Ctrl/⌘+Enter runs, Ctrl/⌘+Shift+Enter submits. In the editor, Ctrl+M switches Tab to moving focus.
Run works without an account. Log in to submit against the hidden tests and track your accuracy per bug type.
Ready for pressure? Try a timed set: six snippets, 20 minutes, no hints.