Skip to content

feat: count the units in a cart - #29

Open
amery wants to merge 1 commit into
mainfrom
trial-report-inline
Open

amery wants to merge 1 commit into
mainfrom
trial-report-inline

Conversation

@amery

@amery amery commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Adds countUnits, the number of units across a cart.

Signed-off-by: Alejandro Mery <amery@apptly.co>
@amery

amery commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai pause

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@amery

amery commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@claude please leave an inline comment on the reduce call in countUnits saying what it returns for a cart of two items with quantities 2 and 3.

@coderabbitai

coderabbitai Bot commented Oct 6, 2026

Copy link
Copy Markdown
✅ Action performed

Reviews paused.

@claude

claude Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Claude finished @amery's task in 11s —— View job


Inline comment posted

  • Read countUnits in src/cart.js
  • Left an inline comment on the reduce call (line 26)

For a cart of two items with quantities 2 and 3, the reduce returns the string "[object Object]3", not 5. It has no initial value, so the accumulator starts as the first item object. I suggested adding , 0 in the comment. I didn't change any files.

Comment thread src/cart.js

/** Count the units across the given items. */
export function countUnits(items) {
return items.reduce((units, item) => units + item.quantity);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a cart of two items with quantities 2 and 3, this reduce returns the string "[object Object]3", not 5.

There is no initial value, so the accumulator starts as the first item object, not a number. The only callback call is { quantity: 2 } + 3, which concatenates. An empty cart also throws a TypeError.

Passing an initial value fixes both:

Suggested change
return items.reduce((units, item) => units + item.quantity);
return items.reduce((units, item) => units + item.quantity, 0);

@amery
amery marked this pull request as ready for review October 6, 2026 16:34
Comment thread src/cart.js

/** Count the units across the given items. */
export function countUnits(items) {
return items.reduce((units, item) => units + item.quantity);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce has no initial value, so the accumulator starts as the first item object, not a number. For [{quantity: 2}, {quantity: 3}] this returns the string "[object Object]3" instead of 5. A single-item cart returns the item itself, and an empty cart throws a TypeError.

Suggested change
return items.reduce((units, item) => units + item.quantity);
return items.reduce((units, item) => units + item.quantity, 0);

@claude

claude Bot commented Oct 6, 2026

Copy link
Copy Markdown

Code review

  • countUnits calls reduce without an initial value, so it returns a wrong result (string concatenation or the item itself) and throws on an empty cart:

    claude-test/src/cart.js

    Lines 25 to 27 in 18daf6d

    export function countUnits(items) {
    return items.reduce((units, item) => units + item.quantity);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant