Home >Web Front-end >JS Tutorial >Use hexadecimal conversion to compress digital functions to share_javascript skills

Use hexadecimal conversion to compress digital functions to share_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:401278browse

Copy code The code is as follows:

function zipNum(num, radix){
if (!zipNum.zip){
zipNum.zip = function(inputNum){
if(inputNum > 35){//Use capital letters to represent 36-61
return String.fromCharCode('A' .charCodeAt (0) inputNum % 36); 10 ); 🎜> if(radix > 36 && radix < 63){
result = [];
do {
remainder = quotient % radix;
quotient = parseInt(quotient / radix);
result.push(zipNum.zip( REMAINDER);
} While (quotient & gt; radix);
Remainer = Quotient % Radix;
If (Remair! == 0) {
Result.push (zi pnum.zip (remainer) );
}
result = result.reverse().join('');
} else if(radix > 1 && radix < 36){
result = num.toString( radix);
} else {
result = num;
window.console && console.warn('radix argument must be between 2 and 62')
}
return result;
}

function testSortNum(){
assertEquals(zipNum(61, 62), 'Z');
assertEquals(zipNum(62 * 2 - 1, 62), '1Z') ;
assertEquals(zipNum(62 * 3 - 1, 62), '2Z');
assertEquals(zipNum(1361182624991, 62), 'dXN5fJB');
}
testSortNum();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn