
- 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
Addition multiplication ladder in an array in JavaScriptn
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements.
For example: If the array is −
const arr = [1, 2, 3, 4, 5, 6, 7];
Then the output should be calculated like this −
1*2+3*4+5*6+7 2+12+30+7
And the output should be −
51
Let’s write the code for this function −
Example
The code for this will be −
const arr = [1, 2, 3, 4, 5, 6, 7]; const alternateOperation = arr => { const productArr = arr.reduce((acc, val, ind) => { if(ind % 2 === 1){ return acc; }; acc.push(val * (arr[ind + 1] || 1)); return acc; }, []); return productArr.reduce((acc, val) => acc + val); }; console.log(alternateOperation(arr));
Output
The output in the console −
51
- Related Articles
- Alternate addition multiplication in an array - JavaScript
- Recursive multiplication in array - JavaScript
- Constructing an array of addition/subtractions relative to first array element in JavaScript
- What is Addition Operator (+) in JavaScript?
- Algorithm for matrix multiplication in JavaScript
- What is Multiplication Operator (*) in JavaScript?
- C program for Addition and Multiplication by 2 using Bitwise Operations.
- Count divisors of array multiplication in C++
- What is Addition Assignment Operator (+=) in JavaScript?
- Flatten an array in JavaScript.
- Chunking an array in JavaScript
- Reorder an array in JavaScript
- Explain the commutative and associative properties of addition and multiplication of integers.
- What is Multiplication Assignment Operator (*=) in JavaScript?
- How will addition, subtraction, multiplication and division operator work with date values stored in MySQL table?

Advertisements