-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
algorithm: integer to base #1258
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
Merged
Merged
Changes from all commits
Commits
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,40 @@ | ||
/** | ||
* @function intToBase | ||
* @description Convert a number from decimal system to another (till decimal) | ||
* @param {Number} number Number to be converted | ||
* @param {Number} base Base of new number system | ||
* @returns {String} Converted Number | ||
* @see [HornerMethod](https://en.wikipedia.org/wiki/Horner%27s_method) | ||
* @example | ||
* const num1 = 125 // Needs to be converted to the binary number system | ||
* gornerScheme(num, 2); // ===> 1111101 | ||
* @example | ||
* const num2 = 125 // Needs to be converted to the octal number system | ||
* gornerScheme(num, 8); // ===> 175 | ||
*/ | ||
const intToBase = (number, base) => { | ||
if (typeof number !== 'number' || typeof base !== 'number') { | ||
throw new Error('Input data must be numbers') | ||
} | ||
// Zero in any number system is zero | ||
if (number === 0) { | ||
return '0' | ||
} | ||
let absoluteValue = Math.abs(number) | ||
let convertedNumber = '' | ||
while (absoluteValue > 0) { | ||
// Every iteration last digit is taken away | ||
// and added to the previous one | ||
const lastDigit = absoluteValue % base | ||
convertedNumber = lastDigit + convertedNumber | ||
absoluteValue = Math.trunc(absoluteValue / base) | ||
} | ||
// Result is whether negative or positive, | ||
// depending on the original value | ||
if (number < 0) { | ||
convertedNumber = '-' + convertedNumber | ||
} | ||
return convertedNumber | ||
} | ||
|
||
export { intToBase } |
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,25 @@ | ||
import { intToBase } from '../intToBase' | ||
|
||
describe('Int to Base', () => { | ||
test('Conversion to the binary system', () => { | ||
expect(intToBase(210, 2)).toEqual('11010010') | ||
expect(intToBase(-210, 2)).toEqual('-11010010') | ||
}) | ||
test('Conversion to the system with base 5', () => { | ||
expect(intToBase(210, 5)).toEqual('1320') | ||
expect(intToBase(-210, 5)).toEqual('-1320') | ||
}) | ||
test('Conversion to the octal system', () => { | ||
expect(intToBase(210, 8)).toEqual('322') | ||
expect(intToBase(-210, 8)).toEqual('-322') | ||
}) | ||
test('Output is 0', () => { | ||
expect(intToBase(0, 8)).toEqual('0') | ||
expect(intToBase(0, 8)).toEqual('0') | ||
}) | ||
test('Throwing an exception', () => { | ||
expect(() => intToBase('string', 2)).toThrow() | ||
expect(() => intToBase(10, 'base')).toThrow() | ||
expect(() => intToBase(true, false)).toThrow() | ||
}) | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.