遍历数组比较值:如何修正shouldWeOrderThisCandy函数使其正确返回true?
Fixing the
shouldWeOrderThisCandy Function Let's walk through what's going wrong with your current code and fix it to meet your requirement. Your function is always returning false because of several key issues in how it's structured and how it accesses data.
Here are the problems in your original code:
- Incorrect function parameter: You wrote
function shouldWeOrderThisCandy(inventory, [])— the second parameter should be a variable name (likecandyName) to capture the candy type you're checking, not an empty array. - Undeclared loop variable:
iisn't declared withletorconst, which makes it a global variable and can cause unexpected behavior down the line. - Wrong object property access: You're using
inventory.weeklyAverageandinventory.inStock, butinventoryis an array — you need to access the current element in the loop withinventory[i]first to get the candy object. - Reversed logic: Your condition checks if
weeklyAverage < inStock, but your requirement says to returntruewheninStock < weeklyAverage. - Early return in loop: You return
trueorfalseon the first iteration of the loop, regardless of whether you've found the specific candy you're supposed to check.
Here's the corrected function:
let inventory = [ { candy: "Twizzlers", inStock: 180, weeklyAverage: 200 }, { candy: "Sour Patch Kids", inStock: 90, weeklyAverage: 100 }, { candy: "Milk Duds", inStock: 300, weeklyAverage: 170 }, { candy: "Now and Laters", inStock: 150, weeklyAverage: 40 } ]; function shouldWeOrderThisCandy(inventory, candyName) { // Loop through each item in the inventory array for (let i = 0; i < inventory.length; i++) { const currentCandy = inventory[i]; // First, check if this is the candy we're looking for if (currentCandy.candy === candyName) { // Return true if inStock is less than weeklyAverage, else false return currentCandy.inStock < currentCandy.weeklyAverage; } } // If the candy isn't found in inventory, return false (adjust this if needed) return false; }
How this works:
- Proper parameter handling: The
candyNameparameter captures the specific candy type you want to check. - Declared loop variable:
let iensures the variable is scoped to the loop, avoiding global scope pollution. - Correct property access:
inventory[i]gets the current candy object in the loop, so we can access itscandy,inStock, andweeklyAverageproperties correctly. - Matching the target candy: We first verify that we're looking at the right candy before checking stock levels — no more checking random candies from the array.
- Simplified correct logic: The return statement directly compares
inStocktoweeklyAverageas per your requirement. We don't need extraif/elsesince the comparison itself returns a boolean value. - Fallback for missing candy: If the candy name doesn't exist in the inventory, the function returns
false(you could modify this to throw an error or handle it differently if your use case requires it).
Test it out:
console.log(shouldWeOrderThisCandy(inventory, "Twizzlers")); // true (180 < 200) console.log(shouldWeOrderThisCandy(inventory, "Milk Duds")); // false (300 > 170) console.log(shouldWeOrderThisCandy(inventory, "Sour Patch Kids")); // true (90 < 100) console.log(shouldWeOrderThisCandy(inventory, "Now and Laters")); // false (150 > 40)
内容的提问来源于stack exchange,提问作者Maraecia Washington
相关产品推荐
相关产品推荐

