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

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions Conversions/DecimalToBinary.js
View file Open in desktop
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)
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable.

while (dec != 0) {
rem = dec % 2;
dec = parseInt(dec / 2);
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a hacky reimplementation of decimalToBinary to me, except instead of using an array to store the digits this uses a base 10 number which may then be stringified in base 10 to produce the desired binary number.

The parseInt hack to achieve floor division rather than using dec >>= 1 is a clear downgrade. You could consider Math.floor(dec / 2) for readability, but relying on parseInt to perform flooring is hacky IMO.

bin = bin + rem * i;
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use += here.

i = i * 10;
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and *= here

}
return bin
}


export { decimalToBinary, decimalToBinary_2, decimalToBinary_3 }

// > decimalToBinary(2)
// '10'
Expand All @@ -16,4 +38,4 @@ export { decimalToBinary }
// '111'

// > decimalToBinary(35)
// '100011'
// '100011'
Copy link
Collaborator

@appgurueu appgurueu Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert to proper tests instead of removing the trailing newline (?)

AltStyle によって変換されたページ (->オリジナル) /