Movatterモバイル変換


[0]ホーム

URL:


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

Commit8f41dfd

Browse files
committed
Base Conversion 💥
1 parent789516c commit8f41dfd

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+
varStack=require('../datastructures/stack');
2+
3+
functionbaseConversion(num,base){
4+
vars=newStack();
5+
while(num){
6+
s.push(num%base);
7+
num=Math.floor(num/base);
8+
}
9+
varans="";
10+
while(!s.empty())
11+
ans+=s.pop();
12+
returnans;
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+
varexpect=require('chai').expect;
2+
varbaseConversion=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)

[8]ページ先頭

©2009-2025 Movatter.jp