5 kyu
Number.prototype.toDecimal
149 of 152wthit56
Loading description...
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
No sample tests
JS Node 12 should be enabled
Tests dont check for negative numbers in Javascript
Good point! I've added negative tests throughout. Thanks for the help.
My solution, which does not handle positive exponents at all, should not be able to pass the tests. In your test cases you should use Test.assertEquals instead of Test.expect.
Ah! Thanks, Balkoth!
WHat's to be made of this "Test Failed: (1e-7).toDecimal() returned 0.014285714285714285 - Expected: 1e-7, instead got: 0.014285714285714285" ?
1e-7
will move the decimal place to the left 7 places. Resulting in the number 0.0000001. So the returned string should be "0.0000001", not "0.014285714285714285". I have tweaked the test messages to hopefully be clearer as to what's going on. Let me know if this helped.For some reason, without making any changes, it passes all the tests now. Thanks. My confusion was with having both "returned", "expected" and then "instead got" in the message. When I read it back now it makes more sense.
Yeah-- understandable. Glad it makes more sense now. ;P
One of the examples is wrong.
It says
0.001 == 1e-2
, but my JS console (and my high school math) say1e-2 == 0.01
Yeah-- that's right. Thanks for your help!
Please don't kill me Thomas ;P. It's a nice kata but error description isn't much helpful: as you can see from previous post, expected value is still in exponential notation. Also, if you take a look at my solution, it has passed even if incomplete. In your tests there are no cases where mantissa is longer than exponent value (eg: 1.234e2). Yep, I've been lucky with the random cases, lol.
Yeah... the trouble is, I can't really figure out what the output should be, otherwise the whole solution would be part of the test, which could be hacked, I think. I've tweaked it so that is shows you the number representation that the toDecimal method would give, next to the expected test number. This means they'll both show the exponential notation form, but at least it'll be clear what's going on. Also, in the error message I've added a print-out of the input number and returned string.
Oh, and I added a case where the coefficient (left bit; it's based on scientific notation, I think) is longer in string form that the exponent. Not sure why that particularly would trip up a solution, but I guess it did yours ;P
Hope that covers everything? Let me know if there's anything else. And thanks for your help. Always appreciated! ^^
Why this?
Test Failed: 1e+21 !== 10000000000000000000 - Expected: 1e+21, instead got: 10000000000000000000
The description says nothing about numbers with (large) positive exponents sould be returned as "itself" (Expected: 1e+21).
Uh - think I got it (really have to learn to count zeros)...
This comment has been hidden.
Good points. I've now added a regex check to ensure the returned value is a valid decimal number, and also a string.
Yeah-- I originally had it as a simple function that took a number as an argument, but went for the prototype method route instead. I've fixed that, now.
Thanks for the help!