-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Create AnyBase #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Create AnyBase #1534
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a2f5519
Create AnyBase
imrahulkb 9335f23
Update AnyBase
imrahulkb c9b7898
Update and rename AnyBase to convertBase
imrahulkb b9095cd
Update convertBase
imrahulkb 4c56cec
Rename convertBase to ConvertBase
imrahulkb 2231fb8
Create ConvertBase.test.js
imrahulkb d02f2d9
Update ConvertBase
imrahulkb ef4e3b4
Update ConvertBase.test.js
imrahulkb 1b4aebd
Update ConvertBase.test.js
imrahulkb 1bde9dc
Update ConvertBase.test.js
imrahulkb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const microConvert = (n, base1, base2) => { | ||
let converted = 0; | ||
let multiplier = 1; | ||
while (n) { | ||
converted += (n % base2) * multiplier; | ||
multiplier *= base1; | ||
n = Math.floor(n / base2); | ||
} | ||
return converted; | ||
}; | ||
|
||
const convertBase = (num, frombase, tobase) => { | ||
return microConvert(microConvert(num, frombase, 10), 10, tobase); | ||
}; | ||
|
||
export { convertBase }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { convertBase } from '../ConvertBase' | ||
|
||
test('Converting number 10010 from base 4 to base 10 should be 260', () => { | ||
const res = convertBase(10010, 4, 10) | ||
expect(res).toBe(260) | ||
}) | ||
|
||
test('Converting number 1101 from base 2 to base 10 should be 13', () => { | ||
const res = convertBase(1101, 2, 10) | ||
expect(res).toBe(13) | ||
}) | ||
|
||
test('Converting number 213 from base 5 to base 8 should be 53', () => { | ||
const res = convertBase(213, 5, 8) | ||
expect(res).toBe(72) | ||
}) | ||
|
||
test('Converting number 37 from base 8 to base 3 should be 2111', () => { | ||
const res = convertBase(37, 8, 3) | ||
expect(res).toBe(1011) | ||
}) | ||
|
||
test('Converting number 123 from base 6 to base 4 should be 33', () => { | ||
const res = convertBase(123, 6, 4) | ||
expect(res).toBe(303) | ||
}) | ||
|
||
test('Converting number 1010 from base 2 to base 9 should be 74', () => { | ||
const res = convertBase(1010, 2, 9) | ||
expect(res).toBe(11) | ||
}) | ||
|
||
test('Converting number 731 from base 7 to base 5 should be 2121', () => { | ||
const res = convertBase(731, 7, 5) | ||
expect(res).toBe(2430) | ||
}) | ||
|
||
test('Converting number 345 from base 9 to base 10 should be 344', () => { | ||
const res = convertBase(345, 9, 10) | ||
expect(res).toBe(284) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.