aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-11 19:34:37 +0100
committerAbigail <abigail@abigail.be>2021-01-11 19:34:37 +0100
commit9e16b4f7196cb9ff5880b5e8d6a28b79d3057b8e (patch)
tree1bdbeecd3a57a2e1aba94197b17d7d29c1924c4d
parent6e4c96b475edc880be09ef9b41addf2737da2446 (diff)
downloadperlweeklychallenge-club-9e16b4f7196cb9ff5880b5e8d6a28b79d3057b8e.tar.gz
perlweeklychallenge-club-9e16b4f7196cb9ff5880b5e8d6a28b79d3057b8e.tar.bz2
perlweeklychallenge-club-9e16b4f7196cb9ff5880b5e8d6a28b79d3057b8e.zip
Node solution week 95/part 2.
-rw-r--r--challenge-095/abigail/node/ch-2.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-095/abigail/node/ch-2.js b/challenge-095/abigail/node/ch-2.js
new file mode 100644
index 0000000000..57f01e039a
--- /dev/null
+++ b/challenge-095/abigail/node/ch-2.js
@@ -0,0 +1,27 @@
+let stack = [];
+let ERROR = "Stack is empty";
+
+ require ("fs")
+. readFileSync (0) // Read all.
+. toString () // Turn it into a string.
+. split ("\n") // Split on newlines.
+. filter (_ => _ . length) // Filter out empty lines.
+
+. map (command => {
+ //
+ // Parse the input line
+ //
+ let [m, operator, value] = command . match (/^(push|pop|top|min)\s*(.*)$/);
+
+ //
+ // Do the actions
+ //
+ if (operator == "push") {stack . push (+value)}
+ if (operator == "pop") {stack . pop ()}
+ if (operator == "top") {
+ console . log (stack . length ? stack [stack . length - 1] : ERROR)
+ }
+ if (operator == "min") {
+ console . log (stack . length ? Math . min (... stack) : ERROR)
+ }
+});