JSFiddle - React, Tailwind, and code Playground
by JInks Peng
JavaScript
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
this.clear =clear;
}
function push(elem) {
this.dataStore[this.top++] = elem;
}
function pop() {
var data = this.dataStore[--this.top];
this.dataStore.length = this.top;
return data;
}
function peek() {
return this.dataStore[this.top--];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
function mulBase(num,base) {
var s = new Stack();
do {
s.push(num % base);
num = Math.floor(num /= base);
} while(num > 0);
var converted = "";
while(s.length() > 0) {
converted += s.pop();
}
return converted;
}
var num = 32,base = 2,
newNum = mulBase(num,base);
console.log(num + " converted to base " + base + " is " + newNum);
var num = 125,base = 8,
newNum = mulBase(num,base);
console.log(num + " converted to base " + base + " is " + newNum);