【问题标题】:Javascript - Food totals array crashingJavascript - 食物总数数组崩溃
【发布时间】:2016-08-29 19:42:37
【问题描述】:

我有一个食物总数的 JS 对象

foodData: [
    { biscuits: 2, cola:2, sandwiches:0, cake:0 },
    { biscuits: 2, cola:2, sandwiches:0, cake:0 },
    { biscuits: 2, cola:0, sandwiches:0, cake:0 },
    {biscuits: 2, cola:0, sandwiches:0, cake:0 },
    {biscuits: 2, cola:0, sandwiches:0, cake:0 },
    {biscuits: 2, cola:0, sandwiches:4, cake:0 },
    {biscuits: 0, cola:0, sandwiches:0, cake:4 }
],

我想要做的是删除每天用户承诺“坏食物”的数量。最终计算节省的卡路里。

项目应该从第一个索引中删除,然后按顺序删除,所以如果他们需要删除 2 位食物,它会尽可能删除饼干,然后是可乐,然后是三明治,最后是蛋糕。

他们可以去除 5 块食物,因此它可能会反复从每种食物中去除 1 块,然后再回到饼干上。这是伯爵应该做的事情

下面的代码可以工作,但在循环时会卡住(超过 3 个项目)。我怀疑这是 if 语句或可能是内部计数。关于如何停止陷入持续循环的任何建议。我想在满足条件时打破 if 语句会有所帮助。

var array_of_functions = [this.totalMonday, this.totalTuesday, this.totalWednesday, this.totalThursday, this.totalThursday,  this.totalFriday, this.totalSaturday, this.totalSunday];

for (i = 0; i < array_of_functions.length; i++) {

  if (array_of_functions[i] > 0) {

    count = 0;
    do {


      if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
        this.foodData[i]['biscuits']--;
        ++count;
      }

      if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
        this.foodData[i]['cola']--;
        ++count;
      }

      if (this.drinksData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
        this.drinksData[i]['sandwiches']--;
        ++count;
      }
      if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
        this.dataData[i]['cake']--;
        ++count;
      }
    } while (count < this.noRemovedFoods)

  }

}

【问题讨论】:

  • 为什么用了两次星期五?
  • 抱歉,错误 - 已删除。
  • 不要修改代码。它使现有答案无效。
  • 代码不是应该的样子
  • jsfiddle 请。否则,问题可能出在哪里还不是很清楚。例如。从上面的代码看来,this.drinksData 甚至不存在。

标签: javascript loops iteration nested-loops


【解决方案1】:

如果您提供更多代码或适当的有意义的代码(例如我不知道drinksData 是什么),那就太好了。我假设您在 do-while 循环中的代码如下所示:

if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
  this.foodData[i]['biscuits']--;
  ++count;
}

if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
  this.foodData[i]['cola']--;
  ++count;
}

if (this.foodData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
  this.foodData[i]['sandwiches']--;
  ++count;
}

if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
  this.foodData[i]['cake']--;
  ++count;
}

在这种情况下,您将陷入无限循环。问题出在您的 do-while 循环条件中。如果所有 foodData items 位都为零,则会出现 count 不会增加的情况,并且由于不满足条件count &lt; this.noRemovedFoods,您将陷入无限循环。

您可以做的一件事是,如果所有食物项目位都归零,则退出 do-while 循环。如果我按照我的理解考虑您的代码,则重写上面的代码以中断 do-while 循环可能如下所示:

do {
  if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
    this.foodData[i]['biscuits']--;
    ++count;
  }

  if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
    this.foodData[i]['cola']--;
    ++count;
  }

  if (this.foodData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
    this.foodData[i]['sandwiches']--;
    ++count;
  }

  if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
    this.foodData[i]['cake']--;
    ++count;
  }

  if(this.foodData[i]['biscuits'] == 0 && this.foodData[i]['cola'] == 0 && this.foodData[i]['sandwiches'] == 0 && this.foodData[i]['cake'] == 0) {
    break;
  }
}

只需检查所有食品位是否都变为零以跳出 do-while 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2010-11-14
    • 2013-08-18
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多