5 kyu

Number.prototype.toDecimal

149 of 152wthit56
Description
Loading description...
Algorithms
  • Please sign in or sign up to leave a comment.
  • ejini战神 Avatar
    • No sample tests

    • JS Node 12 should be enabled

  • challisa Avatar

    Tests dont check for negative numbers in Javascript

  • Balkoth Avatar

    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.

  • lloop Avatar

    WHat's to be made of this "Test Failed: (1e-7).toDecimal() returned 0.014285714285714285 - Expected: 1e-7, instead got: 0.014285714285714285" ?

    • wthit56 Avatar

      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.

    • lloop Avatar

      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.

    • wthit56 Avatar

      Yeah-- understandable. Glad it makes more sense now. ;P

  • nimdahk Avatar

    One of the examples is wrong.

    It says 0.001 == 1e-2, but my JS console (and my high school math) say 1e-2 == 0.01

  • ssineriz Avatar

    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.

    • wthit56 Avatar

      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! ^^

  • christianhammer Avatar

    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).

  • laoris Avatar

    This comment has been hidden.

    • wthit56 Avatar

      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!