【问题标题】:Cumulative sum array in Javascript, one is working and one is notJavascript中的累积和数组,一个有效,一个无效
【发布时间】:2021-08-14 12:56:21
【问题描述】:

我是一个初学者,正在尝试一些关于代码战的 katas。我想知道是否有人可以帮助我并解释这段代码发生了什么。在这一点上,我不是在为整个练习寻找解决方案,我只是想了解为什么 constcumulativeSum 对于两个不同的数组的工作方式不同。

我正在创建两个累积数组,代码适用于第一个(cumulativeProfit 与起始数组 peopleInLine),一切正常,但是当我使用它时对于第二个(cumulativeOutgoings 与起始数组 changeRequired),数字是错误的。

我的 peopleInLine 数组是:[100, 25, 50, 100, 25, 25, 25, 100, 25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100]

我不得不承认,我并不真正理解 const 累积和 = (sum => value => sum += value)(0) 的工作原理。我是在堆栈溢出搜索后找到的!

非常感谢您的帮助。

function tickets(peopleInLine) {
  let changeRequired = [];

  const cumulativeSum = (sum => value => sum += value)(0);

  let cumulativeProfit = peopleInLine.map(cumulativeSum);
  cumulativeProfit.splice(0, 0, 0);
  cumulativeProfit.pop();
  //return cumulativeProfit;
  //logs to console [0, 100, 125, 175, 275, 300, 325, 350, 450, 475, 525, 550, 650, 675, 700, 750, 850, 875, 900, 950]

  for (let i = 0; i < peopleInLine.length; i++) {
      if (peopleInLine[i] === 25) { changeRequired.push(0) }
      else if (peopleInLine[i] === 50) { changeRequired.push(25) }
      else if (peopleInLine[i] === 100) { changeRequired.push(75) }
  };
  //return changeRequired; 
  //correctly logs to console: [75, 0, 25, 75, 0, 0, 0, 75, 0, 25, 0, 75, 0, 0, 25, 75, 0, 0, 25, 75]

  let cumulativeOutgoings = changeRequired.map(cumulativeSum);
  cumulativeOutgoings.splice(0, 0, 0);
  cumulativeOutgoings.pop();
  return cumulativeOutgoings;
  //incorrectly logs to console: [0, 1125, 1125, 1150, 1225, 1225, 1225, 1225, 1300, 1300, 1325, 1325, 1400, 1400, 1400, 1425, 1500, 1500, 1500, 1525] 
  //should be [0, 75, 75, 100, 175,175 etc.]
};
console.log(tickets([100, 25, 50, 100, 25, 25, 25, 100, 25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100]));

【问题讨论】:

  • 我们可以看到错误的输出是什么,但预期的是什么?另外,这段代码应该做什么?
  • 是的,抱歉,如果您滚动浏览,它会在该行的末尾。应该是累积添加changeRequiredArray,开头多了一个0,所以[0, 75, 75, 100, 175, 175, 175, 175, 250, 250, 275, 275, 350, 350, 350, 375, 450、450、450、475]

标签: javascript arrays cumulative-sum


【解决方案1】:

问题在于您使用cumulativeSum 的方式您正在尝试一个函数引用并在两个数组之间使用它,因为当您调用第二个数组时sum 已经有一些价值。修复程序如下所示

function tickets(peopleInLine) {
  let changeRequired = [];

  const cumulativeSum = (sum => value => sum += value);

  let cumulativeProfit = peopleInLine.map(cumulativeSum(0));
  cumulativeProfit.splice(0, 0, 0);
  cumulativeProfit.pop();


  for (let i = 0; i < peopleInLine.length; i++) {
      if (peopleInLine[i] === 25) { changeRequired.push(0) }
      else if (peopleInLine[i] === 50) { changeRequired.push(25) }
      else if (peopleInLine[i] === 100) { changeRequired.push(75) }
  };


  let cumulativeOutgoings = changeRequired.map(cumulativeSum(0));
  cumulativeOutgoings.splice(0, 0, 0);
  cumulativeOutgoings.pop();
  return cumulativeOutgoings;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多