JavaScript: Hamming weight

Last update: 14 Sep 07:42
1
Student
100%
Completion rate

The Hamming weight is the number of 1's of the binary representation of a given number.

solution.js

Write and export as default a function calculating the Hamming weight.

Examples

hammingWeight(0); // 0
hammingWeight(4); // 1
hammingWeight(101); // 4

Tips

  • toString() method will help convert a number into binary

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests