Skip to content

Commit a2f5519

Browse files
authored
Create AnyBase
1 parent f77a970 commit a2f5519

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Conversions/AnyBase

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class AnyBase {
2+
constructor(base1, base2) {
3+
this.base1 = base1;
4+
this.base2 = base2;
5+
}
6+
7+
convert(num) {
8+
const microConvert = (base1, base2, n) => {
9+
let ans = 0;
10+
let multiplier = 1;
11+
while (n) {
12+
ans += (n % base2) * multiplier;
13+
multiplier *= base1;
14+
n = Math.floor(n / base2);
15+
}
16+
return ans;
17+
}
18+
return microConvert(10, this.base2, microConvert(this.base1, 10, num));
19+
}
20+
}
21+
22+
export { AnyBase }

0 commit comments

Comments
 (0)