SOURCE

class Stack {
    constructor() {
        this.arr = []
    }
    push(item) {
        this.arr.push(item)
    }
    pop() {
        return this.arr.pop()
    }
    peek() {
        return this.arr[this.arr.length-1]
    }
    isEmpty() {
        return this.arr.length === 0
    }
    clear() {
        this.arr = []
    }
    size() {
        return this.arr.length
    }
    toString() {
        return this.arr.toString()
    }
}

// let a = new Stack();
// a.push(1)
// a.push(2)
// a.push(4)
// a.pop()

// console.log(a.size())

class StackObj {
    constructor() {
        this.count = 0
        this.obj = {}
    }
    push(element) {
        this.obj[this.count] = element
        this.count ++
    }
    pop() {
        if(this.isEmpty()) {
            return undefined
        }
        this.count --
        const last = this.obj[this.count]
        delete this.obj[this.count]
        return last
    }
    peek() {
        if(this.isEmpty()) {
            return undefined
        }
        return this.obj[this.count-1]
    }
    isEmpty() {
        return this.count === 0
    }
    clear() {
        this.count = 0
        this.obj = {}
    }
    size() {
        return this.count
    }
    toString() {
        if(this.isEmpty()) {
            return ''
        }
        let str = this.obj[0]
        for(let i=1;i<this.count;i++) {
            str += `,${this.obj[i]}`
        }
        return str
    }
}
// let a = new StackObj();
// a.push(1)
// a.push(2)
// a.push(3)
// a.push(4)
// a.pop()
// console.log(JSON.stringify(a.obj))
// console.log(a.toString())

function toBinary(n) {
    let stack = new Stack();
    let rem;
    let binary = '';
    while(n>0) {
        rem = n%2;
        stack.push(rem);
        n = Math.floor(n/2);
    }
    while(stack.size()>0) {
        binary += stack.pop().toString()
    }
    console.log(binary)
}
// toBinary(10)

function toAny(n, base) {
    let stack = new Stack();
    let rem;
    const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let binary = '';
    while(n>0) {
        rem = n%base;
        stack.push(rem);
        n = Math.floor(n/base);
    }
    console.log(stack)

    while(stack.size()>0) {
        binary += digits[stack.pop().toString()]
    }
    console.log(binary)
}
toAny(1012,16)
console 命令行工具 X clear

                    
>
console