Skip to content

Commit ca68f20

Browse files
committed
formatting
1 parent 28e2ac0 commit ca68f20

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

pb1/Problem.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
1+
Given an array of integers, return a new array such that each element at index `i` of the new array is the product of all the numbers in the original array except the one at `i`.
22

3-
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
3+
For example, if our input was `[1, 2, 3, 4, 5]`, the expected output would be `[120, 60, 40, 30, 24]`. If our input was `[3, 2, 1]`, the expected output would be `[2, 3, 6]`.
44

55
Follow-up: what if you can't use division?

pb1/answer.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1+
2+
13
// Time : 06:18
24
const pb1 = ( list ) => {
3-
if(list.length > 0) {
4-
let product = 1;
5-
for(let i=0; i< list.length; i++) {
6-
product *= list[i];
7-
}
5+
let product = 1;
6+
7+
8+
for(let i=0; i< list.length; i++) {
9+
// Product of every element in the input array
10+
product *= list[i];
11+
}
812

9-
for(let i=0; i< list.length; i++) {
10-
list[i] = product / list[i];
11-
}
12-
return list;
13+
for(let i=0; i< list.length; i++) {
14+
// Divide by current index and push
15+
list[i] = product / list[i];
1316
}
14-
return [];
17+
18+
return list;
1519
}
1620

1721
console.log(pb1([1, 2, 3, 4, 5]));
1822

1923
// Bonus Time : 02:34
2024
const pb1_bonus = ( list ) => {
21-
if(list.length > 0) {
22-
const ret = [];
23-
let product = 1;
24-
for(let i=0; i< list.length; i++) {
25-
26-
for(let j=0; j< list.length; j++) {
27-
if(j !== i)
28-
product *= list[j];
29-
}
30-
31-
ret[i] = product;
32-
product = 1;
25+
const ret = [];
26+
let product = 1;
27+
for(let i=0; i< list.length; i++) {
28+
29+
for(let j=0; j< list.length; j++) {
30+
if(j !== i)
31+
product *= list[j];
3332
}
34-
35-
return ret;
36-
33+
34+
ret[i] = product;
35+
product = 1;
3736
}
38-
return [];
37+
38+
return ret;
3939
}
4040

4141
console.log(pb1_bonus([1, 2, 3, 4, 5]));

0 commit comments

Comments
 (0)