-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduct-of-Array-Except-Self-without-constraint.JS
executable file
·51 lines (36 loc) · 1.79 KB
/
product-of-Array-Except-Self-without-constraint.JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Same problem of Finding Product of Array Except Self Javascript - But without Space and Time complexity */
productOfArrElementsExceptSelf = arr => {
let result = [], product;
for (let i = 0; i < arr.length; i++) {
// Super important step, I have to reset the product before the next i value is run
// Else I will get a result of [ 24, 288, 2304, 13824 ], as it will take the previous multiplication value of product
// And also conceptually, when I am keeping one element fixed and calculating the produce ot the other elements - that means
// Before starting the next product calculation, I am assuming the initial product value is 1
product = 1;
for (let j = 0; j < arr.length; j++) {
if (i !== j ) product *= arr[j]
}
result.push(product);
}
return result;
}
// console.log(productOfArrElementsExceptSelf([1, 2, 3, 4]))
// More compact
productOfArrElementsExceptSelf_1 = arr => {
return arr.map((thisItem, index) => {
return arr.reduce((product, value, j) => {
return product * ( index === j ? 1 : value )
}, 1) // Note I am resetting the accumulator(i.e. the product) to 1, for each new element of the map() function
})
}
console.log(productOfArrElementsExceptSelf_1([1, 2, 3, 4]))
/* Syntax of map function
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// Return element for new_array
}[, thisArg])
callback - Function that produces an element of the new Array, taking three arguments:
currentValue - The current element being processed in the array.
indexOptional - The index of the current element being processed in the array.
arrayOptional - The array map was called upon.
thisArgOptional - Value to use as this when executing callback.
*/