-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Binary Convert #865
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
raklaptudirm
merged 3 commits into
TheAlgorithms:master
from
Yatin-kathuria:BinaryConvert
Dec 14, 2021
Merged
Binary Convert #865
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,10 +1,25 @@ | ||
const BinaryConvert = (number) => { | ||
const result = [] | ||
let i | ||
for (i = number; i > 0; i = parseInt(i / 2)) { | ||
result.push(i % 2) // push the value (remainder)to array | ||
} return Number(result.reverse().join('')) | ||
// reverse index of array as string ,join and change the type of value to become Number | ||
/** | ||
* @function BinaryConvert | ||
* @description Convert the decimal to binary. | ||
* @param {Integer} num - The input integer | ||
* @return {Integer} - Binary of num. | ||
* @see [BinaryConvert](https://www.programiz.com/javascript/examples/decimal-binary) | ||
* @example BinaryConvert(12) = 1100 | ||
* @example BinaryConvert(12 + 2) = 1110 | ||
*/ | ||
|
||
const BinaryConvert = (num) => { | ||
let power = 1 | ||
let binary = 0 | ||
|
||
while (num) { | ||
const rem = num % 2 | ||
num = Math.floor(num / 2) | ||
binary = rem * power + binary | ||
power *= 10 | ||
} | ||
|
||
return binary | ||
} | ||
// call function and value as parameter to passing the value | ||
|
||
export { BinaryConvert } |
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 |
---|---|---|
@@ -1,10 +1,22 @@ | ||
import { BinaryConvert } from '../BinaryConvert' | ||
|
||
describe('Binary Convert', () => { | ||
describe('BinaryConvert', () => { | ||
it('should return the correct value', () => { | ||
expect(BinaryConvert(4)).toBe(100) | ||
}) | ||
it('should return the correct value', () => { | ||
expect(BinaryConvert(12)).toBe(1100) | ||
}) | ||
it('should return the correct value of the sum from two number', () => { | ||
expect(BinaryConvert(12 + 2)).toBe(1110) | ||
}) | ||
it('should return the correct value of the subtract from two number', () => { | ||
expect(BinaryConvert(245 - 56)).toBe(10111101) | ||
}) | ||
it('should return the correct value', () => { | ||
expect(BinaryConvert(254)).toBe(11111110) | ||
}) | ||
it('should return the correct value', () => { | ||
expect(BinaryConvert(63483)).toBe(1111011111111011) | ||
}) | ||
}) |
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.