ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับจำนวนเต็ม n และคืนค่า −
- จำนวนเต็ม k ถ้า n เป็นตัวเลขกำลังสอง ดังนั้น k * k ==n หรือ
- ช่วง (k, k+1) โดยที่ k * k
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const num = 83;
const squareRootRange = (num = 1) => {
const exact = Math.sqrt(num);
if(exact === Math.floor(exact)){
return exact;
}else{
return [Math.floor(exact), Math.ceil(exact)];
};
};
console.log(squareRootRange(num)); ผลลัพธ์
ต่อไปนี้เป็นเอาต์พุตคอนโซล -
[9, 10]