I am facing an issue where I need to convert each character into a binary number based on a specific condition. Numbers greater than or equal to 5 should be converted to 1, while numbers less than or equal to 4 should be converted to 0.
Here is the entire code snippet:
n = [
'0110100000', '1001011111',
'1110001010', '0111010101',
'0011100110', '1010011001',
'1101100100', '1011010100',
'1001100111', '1000011000'
] // array of binary
let bin = [] // converting to numbers
length = n.length;
for (var i = 0; i < length; i++)
bin.push(parseInt(n[i]));
var sum = 0; // sum of all binaries
for (var i = 0; i < bin.length; i++) {
sum += bin[i];
}
console.log(sum); // 7466454644
// ...
// code converting each character
// ...
// console.log(sumConverted) // 1011010100
Is there a way for me to convert characters greater than or equal to 5 to 1 and those less than 5 to 0?
For example:
7466454644
7=1, 4=0, 6=1, 6=1, 4=0, 5=1, 4=0, 6=1, 4=0, 4=0
return 1011010100