-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65 Valid Number.js
More file actions
34 lines (29 loc) · 808 Bytes
/
65 Valid Number.js
File metadata and controls
34 lines (29 loc) · 808 Bytes
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
/**
* @param {string} s
* @return {boolean}
*/
var isNumber = function(s) {
s = s.trim();
if (!s.length) return false;
let isDigit = false;
let hasDecimal = false;
let hasE = false;
for (let i = 0; i < s.length; i++) {
const c = s.charAt(i);
if (c === '+' || c === '-') {
if (i !== 0 && s.charAt(i - 1) !== "e") return false;
} else if (c === '.') {
if (hasDecimal || hasE) return false;
hasDecimal = true;
} else if (c === 'e') {
if (hasE || !isDigit) return false;
hasE = true;
isDigit = false;
} else if (/^[0-9]+$/.test(c)) {
isDigit = true;
} else {
return false;
}
}
return isDigit;
};