From 9e16b4f7196cb9ff5880b5e8d6a28b79d3057b8e Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 11 Jan 2021 19:34:37 +0100 Subject: Node solution week 95/part 2. --- challenge-095/abigail/node/ch-2.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-095/abigail/node/ch-2.js 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) + } +}); -- cgit