Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.
Let’s write the code for this function −
Example
const num = '1101';
const n = '11010111';
const inverseBinary = (binary) => {
return binary.split("").map(el => {
return `${1- parseInt(el, 10)}`
}).join("");
};
console.log(inverseBinary(num));
console.log(inverseBinary(n));Output
The output in the console will be −
0010 00101000