เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ตัวเลขสองตัว พูด m และ n และส่งกลับตัวเลขสองตัวที่มีผลรวมเป็น n และผลิตภัณฑ์คือ m หากไม่มีตัวเลขดังกล่าว ฟังก์ชันของเราจะคืนค่าเป็นเท็จ
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
ตัวอย่าง
const perfectNumbers = (sum, prod) => {
for(let i = 0; i < (sum / 2); i++){
if(i * (sum-i) !== prod){
continue;
};
return [i, (sum-i)];
};
return false;
};
// 12 12 are not two distinct numbers
console.log(perfectNumbers(24, 144));
console.log(perfectNumbers(14, 45));
console.log(perfectNumbers(21, 98)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
false [ 5, 9 ] [ 7, 14 ]