เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่มีตัวเลขสองตัว สมมติว่า num1 และ num2
-
ถ้า num1 มากกว่า num2 ฟังก์ชันของเราควรคืนค่ามากกว่า
-
ถ้า num2 มากกว่า num1 ฟังก์ชันของเราจะคืนค่าน้อยกว่า
-
มิฉะนั้น ฟังก์ชันควรกลับมาเท่ากัน
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const compareIntegers = (num1, num2) => {
if(typeof num1 !== 'number' || typeof num2 !== 'number'){
return false;
};
if(num1 > num2){
return 'greater';
}else if(num2 > num1){
return 'smaller';
}else{
return 'equal';
};
};
console.log(compareIntegers(12, 56));
console.log(compareIntegers(72, 56));
console.log(compareIntegers(12, 12));
console.log(compareIntegers(12, 33)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์บนคอนโซล -
smaller greater equal smaller