aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/abigail/node/ch-2.js
blob: 92219dec3f50a9c267be0542d59efc0615b8071e (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
28
29
30
31
32
33
34
35
36
#!/usr/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-2.js < input-file
//

require ('readline')
. createInterface ({input: process . stdin})   
. on ('line', _ => make_hash (+ _))
;


//
// Working from the end of the required string backwards, we alternate
// placing a hash, and placing a number. We place them in an array @out,
// and at the end, print out said array in reverse order.
//
function make_hash (index) {
    let out = []
    let hash = false
    while (index > 0) {
        if (hash = !hash) {
            out . push ("#")
            index --
        }
        else {
            out . push (index + 1)
            index -=   (index + 1) . toString () . length
        }
    }
    console . log (out . reverse () . join (""))
}