Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Addition multiplication ladder in an array in 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, 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 ?
How It Works
The algorithm pairs elements at even indices with their next element, multiplies them, and adds any remaining unpaired element. For odd-indexed elements that are already paired, we skip them.
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
51
Step by Step Explanation
Let's break down how the function processes the array [1, 2, 3, 4, 5, 6, 7]:
const arr = [1, 2, 3, 4, 5, 6, 7];
const alternateOperation = arr => {
console.log("Processing array:", arr);
const productArr = arr.reduce((acc, val, ind) => {
console.log(`Index ${ind}: ${val}`);
if(ind % 2 === 1){
console.log("Odd index, skipping");
return acc;
};
const nextVal = arr[ind + 1] || 1;
const product = val * nextVal;
console.log(`Even index: ${val} * ${nextVal} = ${product}`);
acc.push(product);
return acc;
}, []);
console.log("Products array:", productArr);
const sum = productArr.reduce((acc, val) => acc + val);
console.log("Final sum:", sum);
return sum;
};
alternateOperation(arr);
Output
Processing array: [ 1, 2, 3, 4, 5, 6, 7 ] Index 0: 1 Even index: 1 * 2 = 2 Index 1: 2 Odd index, skipping Index 2: 3 Even index: 3 * 4 = 12 Index 3: 4 Odd index, skipping Index 4: 5 Even index: 5 * 6 = 30 Index 5: 6 Odd index, skipping Index 6: 7 Even index: 7 * 1 = 7 Products array: [ 2, 12, 30, 7 ] Final sum: 51
Alternative Approach
Here's a more straightforward approach using a simple loop:
const alternateOperationSimple = arr => {
let sum = 0;
for(let i = 0; i < arr.length; i += 2) {
if(i + 1 < arr.length) {
sum += arr[i] * arr[i + 1];
console.log(`${arr[i]} * ${arr[i + 1]} = ${arr[i] * arr[i + 1]}`);
} else {
sum += arr[i];
console.log(`Adding remaining element: ${arr[i]}`);
}
}
return sum;
};
console.log("Result:", alternateOperationSimple([1, 2, 3, 4, 5, 6, 7]));
Output
1 * 2 = 2 3 * 4 = 12 5 * 6 = 30 Adding remaining element: 7 Result: 51
Conclusion
The addition multiplication ladder pairs consecutive elements for multiplication, then sums the results. Both the functional and iterative approaches produce the same result, with the iterative version being more readable.
