-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathyoure-a-square.js
53 lines (48 loc) · 1.31 KB
/
youre-a-square.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function isSquare(n) {
if (n === 0) return true;
// iterate up to n
for (let i = 0; i < n; i++) {
// square the current iteration value
// const valueSquared = Math.pow(i, 2);
const valueSquared = i ** 2;
// if the result is equal to n
if (valueSquared === n) {
return true
}
}
return false;
}
function isSquare(n) {
if (n === 0 || n === 1) return true;
for (let i = 0; i <= n / 2; i++) {
const valueSquared = i ** 2;
if (valueSquared === n) {
return true
}
}
return false;
}
function isSquare(n) {
if (n === 0 || n === 1) return true;
for (let i = 0; i <= n / 2; i++) {
const valueSquared = i ** 2;
if (valueSquared === n) {
return true
} else if (valueSquared > n) {
return false;
}
}
return false;
}
function isSquare(n) {
return Number.isInteger(Math.sqrt(n));
}
function isSquare(n) {
return Number.isInteger(n ** (1/2));
}
console.log(isSquare(-1), false, "-1: Negative numbers cannot be square numbers");
console.log(isSquare( 0), true, "0 is a square number (0 * 0)");
console.log(isSquare( 3), false, "3 is not a square number");
console.log(isSquare( 4), true, "4 is a square number (2 * 2)");
console.log(isSquare(25), true, "25 is a square number (5 * 5)");
console.log(isSquare(26), false, "26 is not a square number");