Last active
December 6, 2021 10:26
-
-
Save abhisheks-12/bb903b7fec755e48ea37ceada1e2e959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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