Repository navigation
feat: report the average price per unit #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,3 +20,12 @@ export function applyDiscount(amount, percent) { | |||||
| } | ||||||
| return amount - (amount * percent) / 100; | ||||||
| } | ||||||
|
|
||||||
| /** Average price per unit across the items in a cart. */ | ||||||
| export function averageUnitPrice(items) { | ||||||
| let units = 0; | ||||||
| for (let i = 0; i <= items.length; i++) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Stop the loop before When Proposed fix- for (let i = 0; i <= items.length; i++) {
+ for (let i = 0; i < items.length; i++) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| units += items[i].quantity; | ||||||
| } | ||||||
| return totalItems(items) / units; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Define the result for a cart with zero units. After the loop bound is corrected, an empty cart makes this expression 🤖 Prompt for AI Agents |
||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-by-one:
i <= items.lengthmakes the last iteration readitems[items.length], which isundefined, so.quantitythrows aTypeErroron every call with any input (including an empty array).