Created
September 4, 2014 11:04
-
-
Save AdamMadrzejewski/611f0d7d7b98a6142774 to your computer and use it in GitHub Desktop.
Numeric base converter
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() { | |
function baseConverter(number, base, desiredBase) { | |
var result; | |
function genericToDec (n, dB) { | |
return parseInt(n, dB); | |
} | |
function decToGeneric (n, dB) { | |
return n.toString(dB); | |
} | |
if ((base >= 2 && base <= 16) && (desiredBase >= 2 && desiredBase <= 16)) { | |
if (base == 10) { | |
result = decToGeneric(number, desiredBase); | |
} else { | |
result = decToGeneric(genericToDec(number, base), desiredBase); | |
}; | |
} else { | |
throw new Error("Your initial or final numerical base can't be higher then 16 or lower then 2"); | |
}; | |
return result; | |
}; | |
if (typeof module !== 'undefined') { | |
module.exports = baseConverter; | |
} else { | |
this.baseConverter = baseConverter; | |
} | |
}).call(function() { | |
return this || (typeof window !== 'undefined' ? window : global); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment