Skip to content

Conversation

AanthereX
Copy link

@AanthereX AanthereX commented Aug 2, 2022

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@lgtm-com
Copy link

lgtm-com bot commented Aug 2, 2022

This pull request introduces 1 alert when merging 322a67c into 1a089cc - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@appgurueu appgurueu added the algorithm Adds or improves an algorithm label Aug 3, 2022
Copy link
Collaborator

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

Overall questionable: Second method just lets JS builtins do the job, third one has poor code quality and is mostly a duplicate of the first one. Leaning towards rejecting this - it probably has to be redone pretty much entirely, if it should be redone at all.

// using .toString() method

function decimalToBinary_2(dec) {
return dec.toString(2)
Copy link
Collaborator

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?

@@ -16,4 +38,4 @@ export { decimalToBinary }
// '111'

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

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 (?)

function decimalToBinary_3(dec) {
let bin = 0;
let rem, i = 1,
step = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused variable.

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

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.

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

Choose a reason for hiding this comment

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

You could use += here.

rem = dec % 2;
dec = parseInt(dec / 2);
bin = bin + rem * i;
i = i * 10;
Copy link
Collaborator

Choose a reason for hiding this comment

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

and *= here

@appgurueu appgurueu added the changes required This pull request needs changes label Aug 3, 2022
@raklaptudirm
Copy link
Member

I suggest marking this pr as invalid.

@appgurueu appgurueu added the invalid Doesn't seem right label Aug 4, 2022
@appgurueu appgurueu closed this Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Adds or improves an algorithm changes required This pull request needs changes invalid Doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants