我的代码通过了我所有的测试,但有些 edabit 不认可它
My code passes all my tests but some how edabit is not approving it
function bitwiseAND(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if (+a[i] === 1 && +a[i] === +b[i]) x = x + "1";
else x = x + "0";
}
return +x
}
function bitwiseOR(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if (+a[i] === 1 || +b[i] === 1) x = x + "1";
else x = x + "0";
}
return +x
}
function bitwiseXOR(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if ((+a[i] === 1 && +b[i] === 0) || (+a[i] === 0 && +b[i] === 1))
x = x + "1";
else x = x + "0";
}
return +x
}
挑战是编写三个函数来计算两个数字的按位与、按位或和按位异或。
您的代码很好,唯一缺少的是将二进制转换为 return 值的整数。
代替
return +x
尝试
return parseInt(x, 2)
function bitwiseAND(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if (+a[i] === 1 && +a[i] === +b[i]) x = x + "1";
else x = x + "0";
}
return +x
}
function bitwiseOR(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if (+a[i] === 1 || +b[i] === 1) x = x + "1";
else x = x + "0";
}
return +x
}
function bitwiseXOR(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
if ((+a[i] === 1 && +b[i] === 0) || (+a[i] === 0 && +b[i] === 1))
x = x + "1";
else x = x + "0";
}
return +x
}
挑战是编写三个函数来计算两个数字的按位与、按位或和按位异或。
您的代码很好,唯一缺少的是将二进制转换为 return 值的整数。
代替
return +x
尝试
return parseInt(x, 2)