-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add more functions to DecimalToBinary.js file from Conversions directory #1074
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
function decimalToBinary(num) { | ||
const bin = [] | ||
while (num > 0) { | ||
bin.unshift(num % 2) | ||
num >>= 1 // basically /= 2 without remainder if any | ||
} | ||
return bin.join('') | ||
function decimalToBinary(num) { | ||
const bin = [] | ||
while (num > 0) { | ||
bin.unshift(num % 2) | ||
num >>= 1 // basically /= 2 without remainder if any | ||
} | ||
return bin.join('') | ||
} | ||
|
||
export { decimalToBinary } | ||
// using .toString() method | ||
|
||
function decimalToBinary_2(dec) { | ||
return dec.toString(2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like cheating: The purpose of this repo is to demonstrate how this could be implemented without relying on libraries (including the standard library). Perhaps note the existence of a suitable stdlib func in a comment? |
||
} | ||
|
||
// using Remainder Quotient method | ||
|
||
function decimalToBinary_3(dec) { | ||
let bin = 0; | ||
let rem, i = 1, | ||
step = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused variable. |
||
while (dec != 0) { | ||
rem = dec % 2; | ||
dec = parseInt(dec / 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a hacky reimplementation of The |
||
bin = bin + rem * i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
i = i * 10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||
} | ||
return bin | ||
} | ||
|
||
|
||
export { decimalToBinary, decimalToBinary_2, decimalToBinary_3 } | ||
|
||
// > decimalToBinary(2) | ||
// '10' | ||
|
@@ -16,4 +38,4 @@ export { decimalToBinary } | |
// '111' | ||
|
||
// > decimalToBinary(35) | ||
// '100011' | ||
// '100011' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please convert to proper tests instead of removing the trailing newline (?) |