# Reverse sum of two arrays in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in two arrays of numbers of the same length. The function should return an array with any arbitrary nth element of the array being the sum of nth term from start of first array and nth term from last of second array.

For example −

If the two arrays are −

const arr1 = [34, 5, 3, 3, 1, 6];
const arr2 = [5, 67, 8, 2, 6, 4];

Then the output should be −

const output = [38, 11, 5, 11, 68, 11];

## Example

Following is the code −

const arr1 = [34, 5, 3, 3, 1, 6];
const arr2 = [5, 67, 8, 2, 6, 4];
const reverseSum = (arr1, arr2) => {
const res = [];
for(let i = 0; i < arr1.length; i++){
res[i] = (arr1[i]) + (arr2[arr2.length - i - 1] || 0);
};
return res;
};
console.log(reverseSum(arr1, arr2));

## Output

Following is the output in the console −

[ 38, 11, 5, 11, 68, 11 ]
Published on 18-Sep-2020 09:46:03