-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanswer.js
More file actions
41 lines (29 loc) · 696 Bytes
/
Copy pathanswer.js
File metadata and controls
41 lines (29 loc) · 696 Bytes
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
// Time : 06:18
const pb1 = ( list ) => {
let product = 1;
for(let i=0; i< list.length; i++) {
// Product of every element in the input array
product *= list[i];
}
for(let i=0; i< list.length; i++) {
// Divide by current index and push
list[i] = product / list[i];
}
return list;
}
console.log(pb1([1, 2, 3, 4, 5]));
// Bonus Time : 02:34
const pb1_bonus = ( list ) => {
const ret = [];
let product = 1;
for(let i=0; i< list.length; i++) {
for(let j=0; j< list.length; j++) {
if(j !== i)
product *= list[j];
}
ret[i] = product;
product = 1;
}
return ret;
}
console.log(pb1_bonus([1, 2, 3, 4, 5]));