Javascript中的累加数组,一个在工作,一个不工作
Cumulative sum array in Javascript, one is working and one is not
我是初学者,正在尝试一些代码战中的套路。我想知道是否有人可以帮助我并解释这段代码发生了什么。在这一点上,我不是在为整个练习寻找解决方案,我只是想了解为什么 const cumulativeSum 对于两个不同的数组有不同的工作方式。
我正在创建两个累积数组,代码适用于第一个(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 cumulativeSum = (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]));
问题在于您使用的方式 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;
};
我是初学者,正在尝试一些代码战中的套路。我想知道是否有人可以帮助我并解释这段代码发生了什么。在这一点上,我不是在为整个练习寻找解决方案,我只是想了解为什么 const cumulativeSum 对于两个不同的数组有不同的工作方式。
我正在创建两个累积数组,代码适用于第一个(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 cumulativeSum = (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]));
问题在于您使用的方式 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;
};