
- 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 array JavaScript
We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let’s say first and second and returns a new array that contains,
Sum of first element of first array and last element of second array as first element,
sum of second element of first array and second last element of second array, and so on.
When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Therefore, let's write the code for this function −
Example
const first = [23, 5, 7, 2, 34, 7, 8]; const second = [2, 4, 21, 4, 6, 7, 56, 23, 32, 12]; const reverseSum = (first, second) => { const sumArray = []; let i, j, k; for(i = 0, j = second.length - 1, k = 0; i < first.length && j >= 0; i++, j--, k++){ sumArray[k] = first[i] + second[j]; }; while(i < first.length){ sumArray[k] = first[i]; k++; i++; }; while(j >= 0){ sumArray[k] = second[j]; k++; j--; }; return sumArray; }; console.log(reverseSum(first, second));
Output
The output in the console will be −
[ 35, 37, 30, 58, 41, 13, 12, 21, 4, 2 ]
Let’s see how the output is showing these numbers. For example, 35 above, is the sum of first element of first array and last element of second array as first element, i.e. −
23 (First element of first array) +12 (Last element of second array) ------ 35
- Related Articles
- Reverse index value sum of array in JavaScript
- JavaScript Array reverse()
- Array reverse() in JavaScript
- Reverse sum of two arrays in JavaScript
- Reverse array with for loops JavaScript
- Solution for array reverse algorithm problem JavaScript
- Returning reverse array of integers using JavaScript
- How to reverse the order of a JavaScript array?
- Write a program to reverse an array in JavaScript?
- Array reverse() vs reverse! in Ruby
- How to reverse a portion of an array in JavaScript?
- Consecutive elements sum array in JavaScript
- Recursion - Sum Nested Array in JavaScript
- Absolute sum of array elements - JavaScript
- JavaScript reduce sum array with undefined values

Advertisements