Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f41dfd

Browse files
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /