-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathisPerfectSquare.js
37 lines (32 loc) · 1.01 KB
/
isPerfectSquare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Determine whether the given integer is a perfect square.
*
* An integer `n` is a perfect square if there is some other integer `k`
* such that `k * k === n`. That is, if the square root of `n` is also an
* integer.
*
* Warning: If you use Math.sqrt, you will get incorrect answers. Try to
* find another way.
*
* @example
* isPerfectSquare(-1); // => false (no integer squared can be negative)
* isPerfectSquare(0); // => true
* isPerfectSquare(1); // => true
* isPerfectSquare(2); // => false
* isPerfectSquare(9); // => true (since 3*3 is 9)
*
* @param {number} num - An integer
* @returns {boolean} True if `num` is a perfect square and false otherwise.
*/
function isPerfectSquare(num) {
// A negative number can't be a perfect square, so immediately return false.
if (num < 0) {
return false;
}
// The rest is your job. :)
}
if (require.main === module) {
console.log('Running sanity checks for isPerfectSquare:');
// Your sanity checks go here
}
module.exports = isPerfectSquare;