Skip to content

Instantly share code, notes, and snippets.

@abhisheks-12
Last active December 6, 2021 10:26
Show Gist options
  • Save abhisheks-12/bb903b7fec755e48ea37ceada1e2e959 to your computer and use it in GitHub Desktop.
Save abhisheks-12/bb903b7fec755e48ea37ceada1e2e959 to your computer and use it in GitHub Desktop.
function calsum(arr) {
const tempArr = [];
for (let i = 0; i < arr.length; i++) {
if (i === 0) {
tempArr.push(arr[0]);
}
if (i === 1) {
const sub = arr[1] - arr[0];
console.log(sub);
tempArr.push(sub);
}
if (i === 2) {
tempArr.push(0);
}
if (i === 3) {
const sub = arr[3] - arr[2];
tempArr.push(sub);
}
}
const sum = tempArr.reduce((acc, data) => {
return acc + data;
}, 0);
console.log(sum);
}
calsum([4, 9, 2, 3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment