Skip to content

Shamilist/code-wars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Решение задач на code-wars, JS

Профиль https://www.codewars.com/users/Shamilist

Задача:

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.
Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case.

Решение:

var countBits = function(n) {
let binary = n.toString(2).match(/1/g); 
  if (binary) { 
    return binary.length; 
  }
  return 0; // если 0 
};

About

My code-wars solutions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published