Javascript-食物总数数组崩溃

Javascript - Food totals array crashing

本文关键字:数组 崩溃 数数 Javascript-      更新时间:2023-09-26

我有一个食物总量的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)
  }
}

如果您提供了更多的代码或适当的有意义的代码(例如,我不知道drinksData是什么),那就太好了。我假设你的代码在do-white循环中看起来像这样:

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;
}

在这种情况下,您将陷入一个无限循环。问题出在你边做边循环的条件下。如果所有foodData项的位都为零,则会出现计数不会增加的情况,并且由于不满足条件count < this.noRemovedFoods,您将陷入无限循环。

你可以做的一件事是,如果所有食物的位都为零,就打破"边做边做"的循环。如果我认为你的代码是我所理解的,那么重写上面的代码以打破do-white循环可能看起来像这样:

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;
  }
}

只要检查一下所有的食物位是否都变为零,就可以跳出边做边做的循环。