Home > Web Front-end > JS Tutorial > JavaScript-specific method to count the number of 1's in binary split method_javascript skills

JavaScript-specific method to count the number of 1's in binary split method_javascript skills

WBOY
Release: 2016-05-16 18:28:02
Original
1477 people have browsed it

The code is as follows:

Copy code The code is as follows:

function g(n){
var n = n.toString(2);
var count = 0;
for(var i=0;i{
if(n[i] = = "1")
count ;
}
return count;
}

I thought it was very troublesome to write like this, but I suddenly thought if I could use the split method of js. Realize the calculation of the number of 1, and the parameter of split is regular
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute ]

Discover the original There are two errors in the code.


First, under IE, strings cannot use array subscripts to access the value at a specified position, and can only use the charAt(index) method.

The second is under Chrome and Opera, split(


[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]
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