1.Here is the assignment.
Declare a function or that works like ||, but without using the || operator. /**
2.Here is what tried(actually people gave me advice on return b1 ? b1 : b2.But i couldn't understand it ,and haven't found a proper explanation about it online.
function or(b1, b2) {
if (b1 == false && b2 == false) return false;
else return b1 ? b1 : b2;
}
3.Here are the coding tests, and the code above passed all of them. But would anyone tell me the logic of b1 ? b1 : b2. I am a beginner, please help me !
//TEST 1
actual = or("bananas", false);
expected = "bananas";
if (actual === expected) {
console.log("Yay! Test PASSED.");
} else {
console.error("Test FAILED. Keep trying!");
console.log(" actual: ", actual);
console.log(" expected: ", expected);
}
//TEST 2
actual = or("", "bananas");
expected = "bananas";
if (actual === expected) {
console.log("Yay! Test PASSED.");
} else {
console.error("Test FAILED. Keep trying!");
console.log(" actual: ", actual);
console.log(" expected: ", expected);
}
//TEST 3
actual = or(true, true);
expected = true;
if (actual === expected) {
console.log("Yay! Test PASSED.");
} else {
console.error("Test FAILED. Keep trying!");
console.log(" actual: ", actual);
console.log(" expected: ", expected);
}
//TEST 4
actual = or(true, false);
expected = true;
if (actual === expected) {
console.log("Yay! Test PASSED.");
} else {
console.error("Test FAILED. Keep trying!");
console.log(" actual: ", actual);
console.log(" expected: ", expected);
}
b1 ? b1 : b2
This means that if b1 is true/valid it will simply returns the result due to code of b1 otherwise it will simply return the result due to code of b2
b1 ? b1 : b2 is using a Ternary Operation
If you were to write it as a if-else statement it would look like
if (b1 === true) {
return b1;
} else {
return b2;
}
function or(b1, b2) {
if (b1 == false && b2 == false) return false;
else return b1 ? b1 : b2;
}
Multiple things are happing Headers. Here, You have a test function with falsy check. Falsy is validateNumber, it can be null, blank string or zero. The condition will always return false. If you really looking to test value with the type you should use === instead.
console.log(Boolean("")) //false
console.log(Boolean(null))//false
console.log(Boolean(0))//false
console.log(Boolean(" "))//true
console.log(Boolean("1"))//true