Skip to content

Instantly share code, notes, and snippets.

@Drugak
Created December 23, 2015 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drugak/ac42b213c5962e7648a9 to your computer and use it in GitHub Desktop.
Save Drugak/ac42b213c5962e7648a9 to your computer and use it in GitHub Desktop.
function Stack() {
this.dataStore = [];
this.top = 0;
this.clear = clear;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function peek() {
return this.dataStore[this.top -1];
}
function pop() {
return this.dataStore[--this.top];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
var s = new Stack();
function mulBase(num,base) {
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;
var base = 2;
var newNum = mulBase(num,base);
console.log(num+" converted to base "+base+" is "+newNum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment