
- 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
Product of all other numbers an array in JavaScript
Let’s say, we have to write a function that takes an array of numbers as argument. We have to return a new array with the products of each number except the index we are currently calculating product for.
For example, if arr had 5 indices and we were creating the value for index 1, the numbers at index 0, 2, 3 and 4 would be multiplied. Similarly, if we were creating the value for index 2, the numbers at index 0, 1, 3 and 4 would be multiplied and so on.
Note − It is guaranteed that all the elements inside the array are non-zero.
We will first reduce the array to its product and then we will loop over the array to find the value for that index, we will simply divide the product by the original value at that index.
The code for doing this will be −
Example
const arr = [12, 10, 8, 6, 5, 2]; const produceArray = (arr) => { const product = arr.reduce((acc, val) => acc*val); return arr.map(el => { return product/el; }); }; console.log(produceArray(arr));
Output
The output in the console will be −
[ 4800, 5760, 7200, 9600, 11520, 28800 ]
- Related Articles
- Product of all prime numbers in an Array in C++
- Product of all the Composite Numbers in an array in C++
- Sum of all prime numbers in an array - JavaScript
- Product of numbers present in a nested array in JavaScript
- First digit in product of an array of numbers in C++
- Finding product of an array using recursion in JavaScript
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Compute cartesian product of elements in an array in JavaScript
- Find last k digits in product of an array numbers in C++
- Difference between sum and product of an array in JavaScript
- XOR of all Prime numbers in an Array in C++
- Return the greatest possible product of n numbers from the array in JavaScript
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Equal partition of an array of numbers - JavaScript
- Calculating variance for an array of numbers in JavaScript
