Finding number of digits to be changed to get equivalent binary number in Javascript

The goal is to determine how many digits in the binary number need to be changed to represent the decimal number correctly (either 0 change to 1 or vice versa).

For example: if strArr is [“56”, “011000”] then your program should return 1 because only 1 digit needs to change in the binary number (the first zero needs to become a 1) to correctly represent 56 in binary.

Input: ["5624", "0010111111001"]; Output: 2

Input: ["44", "111111"]; Output: 3

What I have tried so far –

function OffBinary(strArr) { 
  
  let binaryNum = 
    Number(strArr).toString(2).split("").reverse().join("");
  return parseInt(binaryNum + "00", 2); 

}
 
console.log(OffBinary(["5624", "0010111111001"]));
console.log(OffBinary(["44", "111111"]));

Answer

Use this:

function OffBinary(strArr) { 
  let changes = 0;
  Number(strArr[0]).toString(2).split('').forEach((digit, i)=>{
    if (digit != strArr[1].charAt(i)) changes++;         
  });
  return changes;
}