Home >Web Front-end >JS Tutorial >js uses stack to convert decimal to octal and take divisor and remainder_javascript skills

js uses stack to convert decimal to octal and take divisor and remainder_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:44:551264browse
Copy code The code is as follows:

function ten2eight(x){
var s=[];
var r='';
while(x>0){
s.push(x%8);
x=parseInt(x/8);
}
while(s.length>0){
r=r s.pop();
}
return r;
}

N=(N div 8 )*8 (N mod 8) (div is an integer division operation, mod is a remainder)

For an input non-negative decimal integer to be converted into octal, the calculation process is to generate the octal digits sequentially from low to high. , and when outputting, generally speaking, it should be from high to low, which is the opposite of the calculation process.

Note: When taking the divisor, you need to round parseInt
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