aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/abigail/node/ch-2.js
blob: 57f01e039a72520b3712e9bf9893b3e0eb4a5e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)
    }
});