← Back to challenges

Rational Number

JavaScriptHardalgorithmslogicmath

Instructions

Declare a rational() function that gets two natural numbers (a, b) and return a string containing a record of the rational number a / b in the form of a decimal fraction, possibly periodic.

Examples

rational(2, 5) ➞ "0.4"

rational(1, 6) ➞ "0.1(6)"

rational(1, 3) ➞ "0.(3)"

rational(1, 7) ➞ "0.(142857)"

rational(1, 77) ➞ "0.(012987)"

Notes

  • Always a < b.
  • The length of a periodic fraction can be more than 20 numbers
  • You can find a hint in the Comments.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.