-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnumberUtils.js
54 lines (45 loc) · 1.03 KB
/
numberUtils.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
54
/* Utility functions related to numbers */
function isInt(value) {
return !isNaN(value) && (function(x) { return (x | 0) === x; })(parseFloat(value))
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
function isNumeric(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
}
function numericValidator(value) {
if (value) {
if (! /\D/.test(value)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
//return true if the number is odd
function isOdd(num) {
return num % 2 != 0;
}
// return true if the number is even
function isEven(num){
return num % 2 == 0;
}
// return true if the number is prime
function isPrime(num){
if (! num || ! isNumeric(num)) {
return false;
if(num==2)
return true;
for(i=2;i<Math.sqrt(num);i++) {
if(num % i==0)
return false; // otherwise it's a prime no.
}
return true;
}
function getRandomNumber() {
//generates 8 digit random integer as string
return Math.floor((Math.random() * 100000000) + 9999999).toString();
}