aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-10 21:52:24 +0200
committerAbigail <abigail@abigail.be>2021-05-10 21:52:24 +0200
commit2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840 (patch)
treeee7581236dbf5ff09d5c0a4ac888a4e4a6038b62 /challenge-112
parent5af854f4892557e1f1de653b46402cbfd7baaff9 (diff)
downloadperlweeklychallenge-club-2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840.tar.gz
perlweeklychallenge-club-2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840.tar.bz2
perlweeklychallenge-club-2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840.zip
Node.js solutions for week 112
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/abigail/README.md2
-rw-r--r--challenge-112/abigail/node/ch-1.js28
-rw-r--r--challenge-112/abigail/node/ch-2.js20
3 files changed, 50 insertions, 0 deletions
diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md
index 83b5465420..f3d497b249 100644
--- a/challenge-112/abigail/README.md
+++ b/challenge-112/abigail/README.md
@@ -37,6 +37,7 @@ Output: "/a"
* [Bash](bash/ch-1.sh)
* [C](c/ch-1.c)
* [Lua](lua/ch-1.lua)
+* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
### Blog
@@ -57,6 +58,7 @@ This is just finding the `$n + 1` Fibonacci number.
* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
* [Lua](lua/ch-2.lua)
+* [Node.js](node/ch-1.js)
* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-112/abigail/node/ch-1.js b/challenge-112/abigail/node/ch-1.js
new file mode 100644
index 0000000000..ce79942bfe
--- /dev/null
+++ b/challenge-112/abigail/node/ch-1.js
@@ -0,0 +1,28 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-1.js < input-file
+//
+
+ require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => {
+ let parts = _ . split (/\/+/) // Split on slash.
+ let parts2 = []
+ parts . every (_ => {
+ if (_ == "." || _ == "") { // Skip current directory,
+ return true // and empty parts.
+ }
+ if (_ == "..") { // Pop parent directory.
+ parts2 . pop ()
+ return true
+ }
+ parts2 . push (_) // Copy part.
+ return true
+ })
+ console . log ("/" + parts2 . join ("/")) // Print result.
+})
diff --git a/challenge-112/abigail/node/ch-2.js b/challenge-112/abigail/node/ch-2.js
new file mode 100644
index 0000000000..aa80e0970d
--- /dev/null
+++ b/challenge-112/abigail/node/ch-2.js
@@ -0,0 +1,20 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-2.js < input-file
+//
+
+let cache = {0: 1, 1: 1}
+
+function fib (n) {
+ cache [n] = cache [n] || fib (n - 1) + fib (n - 2)
+ return cache [n]
+}
+
+ require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => console . log (fib (+ _)))