aboutsummaryrefslogtreecommitdiff
path: root/challenge-106/abigail/node
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-01 18:00:02 +0200
committerAbigail <abigail@abigail.be>2021-04-01 18:00:02 +0200
commitdb05bd869a464e6656e759c6f422717175eebcc8 (patch)
tree4c398e62b7a21a442f046056bc05fa43eedbde65 /challenge-106/abigail/node
parente807fc6f75dd661c7abf9ff73b1af7932d32a98d (diff)
downloadperlweeklychallenge-club-db05bd869a464e6656e759c6f422717175eebcc8.tar.gz
perlweeklychallenge-club-db05bd869a464e6656e759c6f422717175eebcc8.tar.bz2
perlweeklychallenge-club-db05bd869a464e6656e759c6f422717175eebcc8.zip
Node.js solution for week 106, part 2
Diffstat (limited to 'challenge-106/abigail/node')
-rw-r--r--challenge-106/abigail/node/ch-2.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-106/abigail/node/ch-2.js b/challenge-106/abigail/node/ch-2.js
new file mode 100644
index 0000000000..166a3e050e
--- /dev/null
+++ b/challenge-106/abigail/node/ch-2.js
@@ -0,0 +1,40 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-2.js < input-file
+//
+
+require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => console . log (long_division (... _ . split (" ")
+ . map (_ => +_))))
+
+//
+// See ../README.md for info about this function
+//
+
+function long_division (numerator, denominator) {
+ let BASE = 10
+ let fraction = Math . floor (numerator / denominator) + "."
+ let position = fraction . length
+ let seen = []
+
+ numerator %= denominator
+
+ while (!(numerator in seen)) {
+ if (!numerator) {
+ return (fraction)
+ }
+ seen [numerator] = position
+ fraction += Math . floor (BASE * numerator / denominator)
+ numerator = BASE * numerator % denominator
+ position ++
+ }
+
+ return (fraction . substr (0, seen [numerator]) + "(" +
+ fraction . substr ( seen [numerator]) + ")")
+}