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
Release: 2016-05-16 16:44:55
Original
1255 people have browsed it
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
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template