Challenge: How much of a specified candy should we order this week? We need to know how…
- User
Challenge: How much of a specified candy should we order this week? We need to know how much of a candy to order.
If we have enough in stock (if inStock is greater than weeklyAverage), we should order zero. If we don't have enough in stock, we should order two times the weekly average for that candy. Write a function candyOrderQuantity that takes two arguments:
the inventory array a candy name (string) Return the amount of that candy that we should order.
If there is no candy with that name in the array, return 0.
- Assistant
function candyOrderQuantity(inventory, candyName) { for (let i = 0; i < inventory.length; i++) { if (inventory[i].name === candyName) { if (inventory[i].inStock > inventory[i].weeklyAverage) { return 0; } else { return inventory[i].weeklyAverage * 2; } } } return 0; }
- User
candyOrderQuantity returns 400 for Twizzlers, since the number in stock is less than the weekly average and the weekly average is 200 expected 0 to equal 400 Completed in 2ms returns 200 for Sour Patch Kids, since the number in stock is less than the weekly average and the weekly average is 100 expected 0 to equal 200 Completed in 1ms returns 0 for Now and Laters, since the number in stock is greater than the weekly average returns 0 for Milk Duds, since the number in stock is greater than the weekly average returns 0 for Broccoli Bites, since those aren't in the inventory returns 180 for Kit Kat, when given an alternate inventory expected 0 to equal 180 returns 0 for Twizzlers when given an empty array
- Assistant
It seems like the test cases are not passing. Let's modify the function to fix the issue:
codefunction candyOrderQuantity(inventory, candyName) { for (let i = 0; i < inventory.length; i++) { if (inventory[i].name === candyName) { if (inventory[i].inStock < inventory[i].weeklyAverage) { return inventory[i].weeklyAverage * 2; } else { return 0; } } } return 0; }The above function should fix the code issue mentioned in the test results.