0

I can't split a bunch of numbers when the function is a number - why? How do you do this?

I'm attempting https://leetcode.com/problems/number-of-1-bits/

Ways to split a Number into an Array

Accepted answer:

Well, let's look how it works

num + ''.split('') works like

  1. num is a number
  2. ''.split('') is empty array and it's not a number
  3. so, we have sum of a number and not a number, we will cast num and [] to string
  4. num to string is '969', [] to string is '' (empty)
  5. '969' + '' = '969'

num.toString() + ''.split('') works like

  1. num.toString() is a string
  2. ''.split('') is empty array
  3. so, we have sum of a string and not a string, we will cast [] to string
  4. [] to string is '' (empty)
  5. '969' + '' = '969'

String(num).split('') works like

  1. lets cast num to string
  2. and split it by ''
  3. result of split is array ['9', '6', '9']

Of course when I try it....it doesn't work:

var hammingWeight = function(n) {
    let oneBits = String(n).split('');
    console.log(oneBits)
};

hammingWeight(0000011110)

What's going on here? Why does it not work?

function hammingWeight(n) {
    let oneBits = String(n).split('');
    console.log(oneBits)
};

hammingWeight(0000011110)

Why doesn't this work?

function hammingWeight(n) {
    let oneBits = n.toString().split('');
    console.log(oneBits)
};

hammingWeight(0000011110)

I am beyond confused - where are those numbers coming from?

Binary to String in JavaScript

function hammingWeight(n) {
    const oneBits = String.fromCharCode(
          ...n.split(''))
    )
    console.log(oneBits)
};

hammingWeight(0000011110)

Doesn't work either!

halfer
  • 19,824
  • 17
  • 99
  • 186
kawnah
  • 3,204
  • 8
  • 53
  • 103

1 Answers1

1

The Leet Code website is asking for an integer, but it is counting the number of binary 1 bits.

To satisfy the provided method, you would have to: convert the number into a binary string, split it into individual bits, filter only the 1 bits, and return the length.

/**
 * @param {number} n - a positive integer
 * @return {number} number of 1 bits present in the binary representation
 */
const hammingWeight = n => n.toString(2).split('').filter(v => v === '1').length

console.log(hammingWeight(11))         // 1011 => 3
console.log(hammingWeight(128))        // 10000000 => 1
console.log(hammingWeight(4294967293)) // 11111111111111111111111111111101 => 31

If you want to support binary string input, this is much easier. You just need to take a string argument instead of an integer.

/**
 * @param {string} bin - an unsigned binary integer string
 * @return {number} number of 1 bits present in the binary representation
 */
const hammingWeight = bin => bin.split('').filter(v => v === '1').length

console.log(hammingWeight('1011'))                             //  => 3
console.log(hammingWeight('10000000'))                         //  => 1
console.log(hammingWeight('11111111111111111111111111111101')) //  => 31

Explanation

To elaborate, your issue is that you are sending an octal integer in your example, rather than a binary string.

function hammingWeight(n) {
  let oneBits = String(n).split(''); // ['4','6','8','0']
  console.log(oneBits)
};

hammingWeight(0000011110) // Same as 011110 or 4680

You can send this as a literal binary integer like so, but you still need to stringify it using the radix value of 2 (binary):

function hammingWeight(n) {
  let oneBits = n.toString(2).split(''); // ['1','1','1','1','0']
  console.log(oneBits)
};

hammingWeight(0b0000011110) // Prefix literal binary number with '0b'
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • @Amaarockz the function is requesting a number, not a string. The value `1110` would be the input `14`. That would be an invalid parameter. – Mr. Polywhirl Sep 01 '22 at 15:57
  • the input can't be numbers like that only 0s and 1s – kawnah Sep 01 '22 at 15:58
  • what is 2 doing in toString? – kawnah Sep 01 '22 at 15:59
  • @kawnah The Leet Code website states that the parameter type for `n` is `number`. A string is not a number. See: `@param {number} n - a positive integer` – Mr. Polywhirl Sep 01 '22 at 15:59
  • @kawnah The `2` is the radix. It is telling JS to convert the decimal integer into a binary number string representation. – Mr. Polywhirl Sep 01 '22 at 16:00
  • so can you elaborate in your answer why you can split `bin` like that but I can't with the function? I need to know exactly every detail as to why yours works and mine doesn't – kawnah Sep 01 '22 at 16:07
  • "you would have to: convert the number into a binary string, split it into individual bits" I did literally exactly this and it gave me random numbers - why? – kawnah Sep 01 '22 at 16:08
  • @kawnah I added an "Explanation" section at the bottom. Hopefully this helps. – Mr. Polywhirl Sep 01 '22 at 16:52