I'm trying to do this exercise:
我试着做这个练习:
The challenge is to implement a function which adds all together all the consecutive numbers in an array and pushes them into a new array. example: sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]);
// -> [2,2,4,2,3]
挑战在于实现一个函数,该函数将数组中的所有连续数相加,并将它们推入一个新的数组。例子:sumConsecutives([1,1、2、1,1,1,1,2,- 1,1,1]);/ / - >(2、2、4、2、3)
My idea is to split the array into an array of arrays. So for the above example: [[1,1],[2],[1,1,1,1],[2],[1,1,1]]
then go through and reduce
them.
我的想法是把数组分割成数组。对于上面的例子:[1,1][2][1,1,1,1][2][1,1,1]然后对它们进行简化。
I've tried with a while
loop and push
into a temp variable which then gets pushed if the next number is not the same, to no avail.
我尝试了一个while循环,并将它推入一个临时变量,如果下一个数字不相同,它将被推入。
Any ideas on how to achieve this?
关于如何实现这一点,有什么想法吗?
7 个解决方案
#1
2
The easiest approach I could think of... (without map/reduce, though)
我能想到的最简单的方法是……(虽然没有map / reduce)
var sumConsecutives = function(arr) {
var newArr = [];
var prev = arr[0];
var sum = arr[0];
for (var i=1; i<arr.length; i++){
if (arr[i] !== prev) {
newArr[newArr.length] = sum;
sum = 0;
}
sum += arr[i];
prev = arr[i];
}
// Add last sum
newArr[newArr.length] = sum;
return newArr;
};
console.log ( sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]) );
#2
2
You can use a one step reduce
approach:
你可以使用一个步骤减少方法:
const sumConsecutives = ar =>
ar.reduce((ac, x, i) => {
if ( i !== 0 && ar[i-1] === x)
ac[ac.length -1] += x;
else
ac.push(x);
return ac;
}, [])
var r = sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]); // -> [2,2,4,2,3]
console.log(r)
#3
1
I liked the challange so I made it in a two-step function you thought was useful.
我喜欢challange,所以我用了一个你认为有用的两步函数。
-
The first reduce creates the array of arrays
第一个reduce创建数组的数组。
-
The second reduce sums them up.
第二节总结他们。
It is not the shortest code, but I hope the most understandable.
这不是最短的代码,但我希望最容易理解。
const arr = [1,1,2,1,1,1,1,2,1,1,1];
let currentNumber = undefined;
let currentTempArr = [];
let newArr = arr.reduce((tempArr, value) => {
// check if current number is set
if(currentNumber == undefined) currentNumber = value;
// if current number then push to temp array
if(currentNumber == value)
currentTempArr.push(value);
// else just create a new array and push the old one into the parent array
else {
tempArr.push(currentTempArr);
currentTempArr = [];
currentNumber = value;
currentTempArr.push(value);
}
// return the array back to the next reduce iteration
return tempArr;
}, []);
// push the last temp array, because the function stops before the push
newArr.push(currentTempArr);
// this build your array of arrays
console.info('The array of arrays');
console.log(newArr); // [ [1,1,1], [2], ... ]
// now sum up the sub arrays
let sumArr = newArr.reduce((tempArr, value) => {
let sum = 0;
// for every value in the array we add that to the sum
value.forEach(val => sum += val);
// add the total to the temp array
tempArr.push(sum);
// return the filled array back to the reduce function
return tempArr;
}, []);
// the array with summed up values
console.info('see the magic happen');
console.log(sumArr);
#4
0
function sumConsecutives(input) {
var output = [],
factor = 1,
lastnr = null;
for (var i = 0; i < input.length; i++) {
if (i === 0) {
lastnr = input[i];
continue;
}
if (input[i] !== lastnr) {
output.push(lastnr * factor);
lastnr = input[i];
factor = 1;
} else {
factor++;
}
if (i === (input.length - 1)) {
output.push(input[i] * factor);
}
}
return output;
}
var result = sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]);
#5
0
You can use array.reduce
您可以使用array.reduce
Logic
- Create a temp array.
- 创建一个临时数组中。
- loop over array and check if previous and current value.
- 对数组进行循环,检查之前的值和当前值。
- If same, take last element of temp array and add current value to it.
- 如果相同,取temp数组的最后一个元素并向其添加当前值。
- If not, push current element to temp array
- 如果不是,则将当前元素推到临时数组
Sample
function sumConsecutives(arr) {
var r = [];
arr.reduce(function(p, c, i, a) {
if (p === c) {
r[r.length - 1] += c
} else {
r.push(c)
}
return c;
}, 0);
return r;
}
var a = [1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1];
console.log(sumConsecutives(a));
#6
0
This is the easiest i could think of:
这是我能想到的最简单的方法:
var testarr = [1,1,2,1,1,1,1,2,1,1,1];
var resultarr = [testarr[0]];
for(var i=1; i<testarr.length; i++){
if(testarr[i-1]==testarr[i]){
resultarr[resultarr.length - 1] += testarr[i];
}else{
resultarr.push(testarr[i]);
}
}
console.log(resultarr);
#7
0
You may also try this approach :
你也可以尝试以下方法:
var source = [1,1,2,1,1,1,1,2,1,1,1];
source.reduce(function(p, c, index, arr) {
if (p.length === 0 || arr[index - 1] !== c) {
return p.concat(c);
}
else if (arr[index - 1] === c) {
p[p.length - 1] += c
}
return p;
}, []);
#1
2
The easiest approach I could think of... (without map/reduce, though)
我能想到的最简单的方法是……(虽然没有map / reduce)
var sumConsecutives = function(arr) {
var newArr = [];
var prev = arr[0];
var sum = arr[0];
for (var i=1; i<arr.length; i++){
if (arr[i] !== prev) {
newArr[newArr.length] = sum;
sum = 0;
}
sum += arr[i];
prev = arr[i];
}
// Add last sum
newArr[newArr.length] = sum;
return newArr;
};
console.log ( sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]) );
#2
2
You can use a one step reduce
approach:
你可以使用一个步骤减少方法:
const sumConsecutives = ar =>
ar.reduce((ac, x, i) => {
if ( i !== 0 && ar[i-1] === x)
ac[ac.length -1] += x;
else
ac.push(x);
return ac;
}, [])
var r = sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]); // -> [2,2,4,2,3]
console.log(r)
#3
1
I liked the challange so I made it in a two-step function you thought was useful.
我喜欢challange,所以我用了一个你认为有用的两步函数。
-
The first reduce creates the array of arrays
第一个reduce创建数组的数组。
-
The second reduce sums them up.
第二节总结他们。
It is not the shortest code, but I hope the most understandable.
这不是最短的代码,但我希望最容易理解。
const arr = [1,1,2,1,1,1,1,2,1,1,1];
let currentNumber = undefined;
let currentTempArr = [];
let newArr = arr.reduce((tempArr, value) => {
// check if current number is set
if(currentNumber == undefined) currentNumber = value;
// if current number then push to temp array
if(currentNumber == value)
currentTempArr.push(value);
// else just create a new array and push the old one into the parent array
else {
tempArr.push(currentTempArr);
currentTempArr = [];
currentNumber = value;
currentTempArr.push(value);
}
// return the array back to the next reduce iteration
return tempArr;
}, []);
// push the last temp array, because the function stops before the push
newArr.push(currentTempArr);
// this build your array of arrays
console.info('The array of arrays');
console.log(newArr); // [ [1,1,1], [2], ... ]
// now sum up the sub arrays
let sumArr = newArr.reduce((tempArr, value) => {
let sum = 0;
// for every value in the array we add that to the sum
value.forEach(val => sum += val);
// add the total to the temp array
tempArr.push(sum);
// return the filled array back to the reduce function
return tempArr;
}, []);
// the array with summed up values
console.info('see the magic happen');
console.log(sumArr);
#4
0
function sumConsecutives(input) {
var output = [],
factor = 1,
lastnr = null;
for (var i = 0; i < input.length; i++) {
if (i === 0) {
lastnr = input[i];
continue;
}
if (input[i] !== lastnr) {
output.push(lastnr * factor);
lastnr = input[i];
factor = 1;
} else {
factor++;
}
if (i === (input.length - 1)) {
output.push(input[i] * factor);
}
}
return output;
}
var result = sumConsecutives([1,1,2,1,1,1,1,2,1,1,1]);
#5
0
You can use array.reduce
您可以使用array.reduce
Logic
- Create a temp array.
- 创建一个临时数组中。
- loop over array and check if previous and current value.
- 对数组进行循环,检查之前的值和当前值。
- If same, take last element of temp array and add current value to it.
- 如果相同,取temp数组的最后一个元素并向其添加当前值。
- If not, push current element to temp array
- 如果不是,则将当前元素推到临时数组
Sample
function sumConsecutives(arr) {
var r = [];
arr.reduce(function(p, c, i, a) {
if (p === c) {
r[r.length - 1] += c
} else {
r.push(c)
}
return c;
}, 0);
return r;
}
var a = [1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1];
console.log(sumConsecutives(a));
#6
0
This is the easiest i could think of:
这是我能想到的最简单的方法:
var testarr = [1,1,2,1,1,1,1,2,1,1,1];
var resultarr = [testarr[0]];
for(var i=1; i<testarr.length; i++){
if(testarr[i-1]==testarr[i]){
resultarr[resultarr.length - 1] += testarr[i];
}else{
resultarr.push(testarr[i]);
}
}
console.log(resultarr);
#7
0
You may also try this approach :
你也可以尝试以下方法:
var source = [1,1,2,1,1,1,1,2,1,1,1];
source.reduce(function(p, c, index, arr) {
if (p.length === 0 || arr[index - 1] !== c) {
return p.concat(c);
}
else if (arr[index - 1] === c) {
p[p.length - 1] += c
}
return p;
}, []);