เราต้องเขียนฟังก์ชันที่รับตัวเลขและคืนค่า true หากเป็น Pronic number มิฉะนั้น false
Pronic number คือจำนวนที่เป็นผลคูณของสองจำนวนเต็มติดต่อกัน คือรูปแบบ n(n + 1)
ตัวอย่าง
โค้ดสำหรับนี้:
const num = 132; const isPronic = num => { let nearestSqrt = Math.floor(Math.sqrt(num)) - 1; while(nearestSqrt * (nearestSqrt + 1) <= num){ if(nearestSqrt * (nearestSqrt+1) === num ){ return true; }; nearestSqrt++; }; return false; }; console.log(isPronic(num));ผลลัพธ์
ผลลัพธ์ในคอนโซล:
true