Skip to content

Commit a42befa

Browse files
committed
update example solution
1 parent 3eb6054 commit a42befa

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
const isPalindrome = num => num.toString().split('').reverse().join('') === num.toString();
22

3-
export const generatePalindrome = (params) => {
4-
let maxFactor,
5-
minFactor,
6-
maxProduct,
7-
minProduct,
8-
data;
9-
maxFactor = params.maxFactor;
10-
minFactor = params.minFactor || 1;
11-
maxProduct = 1;
12-
minProduct = Infinity;
13-
data = [];
3+
export class Palindrome {
4+
static generate(params) {
5+
let maxFactor,
6+
minFactor,
7+
maxProduct,
8+
minProduct,
9+
data;
10+
maxFactor = params.maxFactor;
11+
minFactor = params.minFactor || 1;
12+
maxProduct = 1;
13+
minProduct = Infinity;
14+
data = [];
1415

15-
for (let ii = minFactor; ii < maxFactor; ii++) {
16-
for (let jj = ii; jj <= maxFactor; jj++) {
17-
const product = ii * jj;
18-
if (isPalindrome(product)) {
19-
data[product] = [ii, jj];
20-
maxProduct = Math.max(maxProduct, product);
21-
minProduct = Math.min(minProduct, product);
16+
for (let ii = minFactor; ii < maxFactor; ii++) {
17+
for (let jj = ii; jj <= maxFactor; jj++) {
18+
const product = ii * jj;
19+
if (isPalindrome(product)) {
20+
data[product] = [ii, jj];
21+
maxProduct = Math.max(maxProduct, product);
22+
minProduct = Math.min(minProduct, product);
23+
}
2224
}
2325
}
24-
}
2526

26-
return {
27-
largest: {
28-
value: maxProduct,
29-
factors: data[maxProduct],
30-
},
31-
smallest: {
32-
value: minProduct,
33-
factors: data[minProduct],
34-
},
35-
};
36-
};
27+
return {
28+
largest: {
29+
value: maxProduct,
30+
factors: data[maxProduct],
31+
},
32+
smallest: {
33+
value: minProduct,
34+
factors: data[minProduct],
35+
},
36+
};
37+
}
38+
}

0 commit comments

Comments
 (0)