In this challenge, you are tasked with fixing a JavaScript function that processes shopping cart data. The function processCartData is supposed to clean, validate, and calculate the total price of items in a cart. However, it contains several subtle bugs that lead to incorrect results, especially when dealing with edge cases and malformed input.
Your goal is to identify and fix these bugs to ensure the function works correctly for all valid inputs and gracefully handles invalid ones.
You are given a list of cartItems, where each item is an object with the following structure:
{
id: string, // Unique identifier
name: string, // Product name
quantity: string, // Number of items (can be "0", "1", " 2 ", "NaN", etc.)
price: string // Price per item (can be "10.00", " 5 ", "invalid", etc.)
}The processCartData function should perform the following operations:
- Validation:
id: Must be a non-empty string.name: Must be a non-empty string after trimming any leading/trailing whitespace.quantity: Must be a positive integer (e.g., "1", "5", "10"). "0", negative numbers, non-numeric strings, or empty strings are invalid.price: Must be a positive number (e.g., "10.00", "5", "12.50"). "0", negative numbers, non-numeric strings, or empty strings are invalid.
- Filtering: Remove any
cartItemsthat fail validation. - Type Conversion: Convert
quantityandpricefrom strings to their appropriate number types for valid items. - Total Calculation: Calculate the sum of (
quantity*price) for all valid and cleaned items.
The processCartData function should return an object with two properties:
cleanedCart: An array of valid and cleaned cart item objects. Each item in this array should havequantityandpriceas numbers.totalPrice: A number representing the sum of (quantity*price) for all items incleanedCart.
-
Run the tests:
node test.js
The tests are designed to fail initially. Your task is to modify easy.js until all tests in test.js pass.
-
Validate
id- Always
trimbefore validation
- Always
-
Validate
name -
Validate
quantityquantitymust be integer,pricecan be decimal.
-
Validate
price
- Pay close attention to type conversions (especially
Number()behavior with different string inputs like" ","","NaN"). - Consider the order of your validation checks. Does it make sense to convert a value to a number before checking if it's a valid string?
- Review the logic for calculating the total price. Is it correctly multiplying quantity by price for each item?
- Think about edge cases: empty strings, strings with only whitespace, "0" as a number, "NaN" as a number, negative numbers.
Remember: The code is intentionally broken. Your critical thinking and debugging skills are key!