
- 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
Alternate addition multiplication in an array - JavaScript
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, 4, 1, 2, 3, 4, 3];
then the output should be calculated like this −
1*2+4*1+2*3+4*3 2+4+6+12
And the output should be −
24
Example
Let's write the code for this −
const arr = [1, 2, 4, 1, 2, 3, 4, 3]; 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: −
24
- Related Articles
- Addition multiplication ladder in an array in JavaScript\n
- Recursive multiplication in array - JavaScript
- Constructing an array of addition/subtractions relative to first array element in JavaScript
- Alternate casing a string in JavaScript
- C program for Addition and Multiplication by 2 using Bitwise Operations.
- Explain the commutative and associative properties of addition and multiplication of integers.
- How to search the alternate text of an image-map area in JavaScript?
- What is Addition Operator (+) in JavaScript?
- What is Multiplication Operator (*) in JavaScript?
- Algorithm for matrix multiplication in JavaScript
- Count divisors of array multiplication in C++
- Difference of digits at alternate indices in JavaScript
- Flatten an array in JavaScript.
- Chunking an array in JavaScript
- Reorder an array in JavaScript

Advertisements