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
}
challenge was to Write three functions to calculate the bitwise AND, bitwise OR and bitwise XOR of two numbers.
Your code is good, the only thing that is missing is to convert the binary to an integer on your return values.
Insted of
return +x
Try
return parseInt(x, 2)