ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์ของค่าบูลีนและตัวดำเนินการเชิงตรรกะ
ฟังก์ชันของเราควรส่งคืนผลลัพธ์บูลีนตามการใช้ตัวดำเนินการกับค่าในอาร์เรย์ตามลำดับ
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const array = [true, true, false];
const op = 'AND';
function logicalCalc(array, op){
var result = array[0];
for(var i = 1; i < array.length; i++){
if(op == "AND"){
result = result && array[i];
}
if(op == "OR"){
result = result || array[i];
}
if(op == "XOR"){
result = result != array[i];
}
}
return result;
}
console.log(logicalCalc(array, op)); ผลลัพธ์
false