
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Reverse sum of two arrays in JavaScript
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 ]
- Related Articles
- Multiply and Sum Two Arrays in JavaScript
- Reverse sum array JavaScript
- Reverse index value sum of array in JavaScript
- Partial sum in array of arrays JavaScript
- Maximum OR sum of sub-arrays of two different arrays in C++
- isSubset of two arrays in JavaScript
- Equality of two arrays JavaScript
- Intersection of two arrays JavaScript
- Maximum Sum of Products of Two Arrays in C++
- Deviations in two JavaScript arrays in JavaScript
- Joining two Arrays in Javascript
- Combining two arrays in JavaScript
- Balancing two arrays in JavaScript
- Sum JavaScript arrays repeated value
- Sum arrays repeated value - JavaScript

Advertisements