JavaScript 中的逆运算

javascriptweb developmentobject oriented programming更新于 2025/10/1 20:28:17

假设,我们必须编写一个函数,该函数接受一个二进制字符串(仅由 0 和 1 组成) 并返回其逆,所有 0 都替换为 1,所有 1 都替换为 0。

让我们来编写该函数的代码 −

示例

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));

输出

控制台中的输出将是 −

0010
00101000

相关文章


有用资源