function Stack() {
this.arr = [];
Stack.prototype.push = (element) => {
this.arr.push(element);
};
Stack.prototype.pop = () => {
return this.arr.pop();
};
Stack.prototype.peek = () => {
return this.arr[this.arr.length - 1];
};
Stack.prototype.isEmpty = () => {
return this.arr.length == 0;
};
Stack.prototype.size = () => {
return this.arr.length;
};
Stack.prototype.toString = () => {
let resultString = "";
for (let i = 0; i < this.items.length; i++) {
resultString += this.items[i] + " ";
}
return resultString;
};
}
function dec2bin(decNumber) {
const stack = new Stack();
while (decNumber > 0) {
stack.push(decNumber % 2);
decNumber = Math.floor(decNumber / 2);
}
let binaryString = "";
while (!stack.isEmpty()) {
binaryString += stack.pop();
}
return binaryString;
}
console.log("dec2bin(100)", dec2bin(100));
console.log("dec2bin(100)", dec2bin(200));
console.log("dec2bin(100)", dec2bin(20));