Repository navigation
Conversation
Signed-off-by: Alejandro Mery <amery@apptly.co>
|
@coderabbitai pause |
|
Note Reviews pausedUse the following commands to manage reviews:
Use the checkboxes below for quick actions:
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. Comment |
|
@claude please leave an inline comment on the |
✅ Action performedReviews paused. |
|
Claude finished @amery's task in 11s —— View job Inline comment posted
For a cart of two items with quantities 2 and 3, the |
|
|
||
| /** Count the units across the given items. */ | ||
| export function countUnits(items) { | ||
| return items.reduce((units, item) => units + item.quantity); |
There was a problem hiding this comment.
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:
| return items.reduce((units, item) => units + item.quantity); | |
| return items.reduce((units, item) => units + item.quantity, 0); |
|
|
||
| /** Count the units across the given items. */ | ||
| export function countUnits(items) { | ||
| return items.reduce((units, item) => units + item.quantity); |
There was a problem hiding this comment.
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.
| return items.reduce((units, item) => units + item.quantity); | |
| return items.reduce((units, item) => units + item.quantity, 0); |
Code review
|
Adds
countUnits, the number of units across a cart.