Created
December 23, 2015 16:23
-
-
Save Drugak/ac42b213c5962e7648a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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