Implement prototype(items, capacity) and solve(items, capacity). Each item is [weight, value] with positive integer weight and nonnegative integer value. Capacity is nonnegative. Return the maximum total value with each item used at most once; unused capacity and an empty selection are allowed.

[[2, 3], [3, 4], [4, 5]] with capacity 6 returns 8; [[2, 3]] with capacity 4 returns 3. Duplicate records are separate items. Negative capacity, weight<=0 or value<0 throws RangeError.

Prototype: enumerate subsets. Final: O(nC) DP/O(C) auxiliary space with dependency-safe descending updates. Preserve all inputs, including on failure.

All numeric inputs and finite arithmetic results in this lesson are safe-integer Numbers. Use null for documented absence, not undefined or a numeric sentinel. Return actual booleans where the contract requires them. Do not add validation requirements beyond the documented inputs.

Run with Node.js 20+ from this directory:

Shell
node main.mjs --stage prototype
node main.mjs --stage final

Edit exercise.mjs; keep main.mjs unchanged. Both stages begin as unfinished learner functions. The website uses the same test functions in a separate browser worker. Tests check deterministic examples, boundaries, documented validation, input preservation, and stage-specific cases. Passing tests does not prove the required time complexity.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.