You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遍历数组比较值:如何修正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 (like candyName) to capture the candy type you're checking, not an empty array.
  • Undeclared loop variable: i isn't declared with let or const, which makes it a global variable and can cause unexpected behavior down the line.
  • Wrong object property access: You're using inventory.weeklyAverage and inventory.inStock, but inventory is an array — you need to access the current element in the loop with inventory[i] first to get the candy object.
  • Reversed logic: Your condition checks if weeklyAverage < inStock, but your requirement says to return true when inStock < weeklyAverage.
  • Early return in loop: You return true or false on 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:

  1. Proper parameter handling: The candyName parameter captures the specific candy type you want to check.
  2. Declared loop variable: let i ensures the variable is scoped to the loop, avoiding global scope pollution.
  3. Correct property access: inventory[i] gets the current candy object in the loop, so we can access its candy, inStock, and weeklyAverage properties correctly.
  4. 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.
  5. Simplified correct logic: The return statement directly compares inStock to weeklyAverage as per your requirement. We don't need extra if/else since the comparison itself returns a boolean value.
  6. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:02:38