class Stack {
constructor() {
this.items = [];
}
push(ele) {
this.items.push(ele);
};
pop() {
return this.items.pop();
};
peek() {
return this.items[this.items.length-1];
};
isEmpty() {
return this.items.length === 0;
};
clear() {
this.items = [];
};
size() {
return this.items.length;
};
}
class Stack {
constructor() {
this.items = {};
this.count = 0;
}
push(ele) {
this.items[this.count] = ele;
this.count++;
}
size() {
return this.count;
}
isEmpty() {
return this.count === 0;
}
pop() {
if (this.isEmpty()) {
return undefined
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result
}
peek() {
if (this.isEmpty()) {
return undefined
}
return this.items[this.count - 1];
}
clear() {
this.items = {};
this.count = 0;
}
toString() {
if (this.isEmpty()) {
return ''
}
let str = `${this.items[0]}`;
for(let i = 1; i < this.count; i++) {
str = `${str},${this.items[i]}`
}
return str;
}
}
function decimalToBinary(num) {
let stack = new Stack();
let binarystr = '';
let number = num;
let rem;
while(number > 0) {
rem = Math.floor(number%2);
stack.push(rem);
number = Math.floor(number/2);
}
while(!stack.isEmpty()) {
binarystr += stack.pop().toString();
}
return binarystr
}
function decimalToBinary2(num, base) {
let stack = new Stack();
let binarystr = '';
let number = num;
let digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let rem;
if (!(base >= 2 && base < 36)) {
return '';
}
while(number > 0) {
rem = Math.floor(number%base);
stack.push(rem);
number = Math.floor(number/base);
}
while(!stack.isEmpty()) {
binarystr += digits[stack.pop()]
}
return binarystr
}