JavaScript
JavaScript copied to clipboard
feat: add decimal to fraction algorithm
Describe your change:
- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?
Checklist:
- [x] I have read CONTRIBUTING.md.
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new JavaScript files are placed inside an existing directory.
- [x] All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare not - [x] All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains
Fixes: #{$ISSUE_NO}.
I'd like to reiterate this:
Please make clear what the spec of this is. What is the accuracy? If it's in decimal places, why do you need any complicated logic beyond rounding, and choosing the appropriate power of ten for the denominator?
Code-wise, it seems you misunderstood my point. I'm asking for a "flattening" of the code using early returns / throws. The logic should be something like "if input is invalid, throw. if input is trivial (integer), return early. now do the actual meaty logic (if we got here, we didn't return or throw already)".
I'd like to reiterate this:
Please make clear what the spec of this is. What is the accuracy? If it's in decimal places, why do you need any complicated logic beyond rounding, and choosing the appropriate power of ten for the denominator?
Code-wise, it seems you misunderstood my point. I'm asking for a "flattening" of the code using early returns / throws. The logic should be something like "if input is invalid, throw. if input is trivial (integer), return early. now do the actual meaty logic (if we got here, we didn't return or throw already)".
If we got the number which is not an integer, then what!? Or if the is input is not a number even, then I will wait for what? Please clear me.😟
If we got the number which is not an integer, then what!? Or if the is input is not a number even, then I will wait for what? Please clear me.😟
The logic should stay the same, it's just about how you write it. Currently the code is a mess of if-elses - e.g., elses to throw in error cases. What I'm proposing is to use early returns and throws to clean it up structure-wise. Suppose you have
function decimalToFraction(decimal) {
if (typeof decimal === "number") {
if (Number.isInteger(decimal)) {
return [decimal, 1]
} else {
// this is where the logic for converting a valid decimal that isn't an integer to a fraction goes...
}
} else {
throw "invalid type"
}
}
then this would be better written as
function decimalToFraction(decimal) {
if (typeof decimal !== "number") throw "invalid type"
if (Number.isInteger(decimal)) return [decimal, 1]
// this is where the logic for converting a valid decimal that isn't an integer to a fraction goes...
}
@appgurueu fixed all issues you have mentioned.
@appgurueu any progress?!
@appgurueu waiting for your updated review.