Skip to content

Commit 8f41dfd

Browse files
committed
Base Conversion 💥
1 parent 789516c commit 8f41dfd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

examples/usecases/baseConversion.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Stack = require('../datastructures/stack');
2+
3+
function baseConversion(num, base) {
4+
var s = new Stack();
5+
while(num) {
6+
s.push(num % base);
7+
num = Math.floor(num / base);
8+
}
9+
var ans = "";
10+
while(!s.empty())
11+
ans += s.pop();
12+
return ans;
13+
}
14+
15+
module.exports = baseConversion;

test/usecases/baseConversion.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var expect = require('chai').expect;
2+
var baseConversion = require('../../examples/usecases/baseConversion');
3+
4+
describe('=> BASE CONVERSION', function() {
5+
6+
it('should convert 4578 to the base 10 to be equal to 10', function(done) {
7+
expect(baseConversion(4578,5)).to.equal("121303");
8+
done();
9+
});
10+
11+
it('should convert 3 to the base 3 to be equal to 10', function(done) {
12+
expect(baseConversion(3,3)).to.equal("10");
13+
done();
14+
});
15+
16+
it('should convert 4 to the base 2 to be equal to 100', function(done) {
17+
expect(baseConversion(4,2)).to.equal("100");
18+
done();
19+
});
20+
21+
it('should convert 1048576 to the base 2 to be equal to 100000000000000000000', function(done) {
22+
expect(baseConversion(1048576,2)).to.equal("100000000000000000000");
23+
done();
24+
});
25+
26+
});

0 commit comments

Comments
 (0)