Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
//var initialData = [1, 9, 4, 95, -3, 51, 6, 9, 4, 4,-9, 88, 27];
var initialData = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
var wayFirst = function(data) {
var sum = 0;
data.forEach( (value) => {sum = sum + value} )
return sum;
}
var waySecond = function(data) {
var sum = 0;
data.forEach( (value, i) => {
i % 2 === 0 ? sum = sum + value : false
})
return sum;
}
var wayThird = function(data) {
var sum = 0;
var iTemp = 0
data.forEach( (value, i) => {
if (!iTemp || iTemp + 4 === i) {
sum = sum + value;
iTemp === 0 ? iTemp = 1 : iTemp = i;
}
})
return sum;
}
console.log(wayFirst(initialData));
console.log(waySecond(initialData))
console.log(wayThird(initialData))
const initialData = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
let sumArrayWithInterval = (data, interval) => {
const sumWithIntervel = [];
let next = 0;
data.forEach((value, i) => {
if (next === i) {
const valueIndex = interval === 1 ? 1 + i : interval + 1 + i;
data[valueIndex] ? sumWithIntervel.push(value + data[valueIndex]) : false
interval === 1 ? next = i + interval + 1 : next = valueIndex + 1
}
})
return sumWithIntervel;
}
console.log(
sumArrayWithInterval(initialData, 1)
);
console.log(
sumArrayWithInterval(
sumArrayWithInterval(initialData, 1)
, 2)
);
console.log(
sumArrayWithInterval(
sumArrayWithInterval(
sumArrayWithInterval(initialData, 1)
, 2)
, 4)
);
I recommend doing this during the day.
You open the curtains of the window to make it brighter.
You can also turn on the light.
You take it, you pour out the whole array on the table.
You move some elements away from others with your fingers.
See to it that the elements are good.
Well, good ones right from the table you throw your finger into the pan.
But seriously, first formulate more clearly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question