
- 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
Constructing product array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The function should construct a new array based on the original array. Each corresponding element of the new array should be the product of all the elements of the original array including that element.
For example −
If the input array is −
const arr = [1, 2, 3, 4, 5];
Then the output array should be −
const output = [120, 60, 40, 30, 24];
We have to achieve this in linear time and constant space (obviously excluding the space used up in constructing the new array).
Example
Following is the code −
const arr = [1, 2, 3, 4, 5]; const exclusiveProduct = (arr = []) => { // O(n) time complexity const product = arr.reduce((acc, val) => acc * val); const res = []; // O(n) time complexity for(let i = 0; i < arr.length; i++){ const el = arr[i]; res[i] = product / el; }; return res; }; console.log(exclusiveProduct(arr));
Output
Following is the output on console −
[120, 60, 40, 30, 24]
- Related Articles
- Constructing multiples array - JavaScript
- Constructing array from string unique characters in JavaScript
- Constructing largest number from an array in JavaScript
- Constructing 2-D array based on some constraints in JavaScript
- Constructing an array of addition/subtractions relative to first array element in JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- Constructing an array of first n multiples of an input number in JavaScript
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Constructing a nested JSON object in JavaScript
- Finding the Largest Triple Product Array in JavaScript
- Omitting false values while constructing string in JavaScript
- Product of all other numbers an array in JavaScript
- Finding product of an array using recursion in JavaScript
- Product of numbers present in a nested array in JavaScript

Advertisements