|
| 1 | +/** |
| 2 | + * @param {string} s |
| 3 | + * @return {boolean} |
| 4 | + */ |
| 5 | +let hasADot = 0; |
| 6 | +let hasAnE = 0; |
| 7 | + |
| 8 | +var isNumber = function(s) { |
| 9 | + hasADot = 0; |
| 10 | + hasAnE = 0; |
| 11 | + s = s.trim(); |
| 12 | + return validateForFirstCharacter(s); |
| 13 | +}; |
| 14 | + |
| 15 | +let validateForFirstCharacter = (s) => { |
| 16 | + if(validateFirstCharacter(s[0])) { |
| 17 | + if(!isNaN(s[0])) |
| 18 | + return validateForNumber(s, 0); |
| 19 | + else if(s[0] === "+" || s[0] === "-") |
| 20 | + return validateForSign(s, 0); |
| 21 | + else if(s[0] === ".") |
| 22 | + return validateForDot(s, 0); |
| 23 | + else |
| 24 | + return false; |
| 25 | + } else |
| 26 | + return false; |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +let validateFirstCharacter = (fc) => { |
| 31 | + return (!isNaN(fc) || fc === "+" || fc === "-" || fc === "." || fc === " "); |
| 32 | +} |
| 33 | + |
| 34 | +let validateForNumber = (currentString, currentIndex) => { |
| 35 | + let nextCharacter = currentString[currentIndex + 1]; |
| 36 | + |
| 37 | + if(nextCharacter === undefined) |
| 38 | + return true; |
| 39 | + else if(nextCharacter === " ") |
| 40 | + return false; |
| 41 | + else if(nextCharacter === ".") |
| 42 | + return validateForDot(currentString, currentIndex + 1); |
| 43 | + else if(nextCharacter === "e") |
| 44 | + return validateForE(currentString, currentIndex + 1); |
| 45 | + else if(!isNaN(nextCharacter)) |
| 46 | + return validateForNumber(currentString, currentIndex + 1); |
| 47 | + else |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +let validateForSign = (currentString, currentIndex) => { |
| 52 | + let nextCharacter = currentString[currentIndex + 1]; |
| 53 | + |
| 54 | + if(nextCharacter === "." && !isNaN(currentString[currentIndex + 2])) |
| 55 | + return validateForDot(currentString, currentIndex + 1); |
| 56 | + else if(!isNaN(nextCharacter) && nextCharacter !== " ") |
| 57 | + return validateForNumber(currentString, currentIndex + 1); |
| 58 | + else |
| 59 | + return false; |
| 60 | +} |
| 61 | + |
| 62 | +let validateForDot = (currentString, currentIndex) => { |
| 63 | + let nextCharacter = currentString[currentIndex + 1]; |
| 64 | + |
| 65 | + if(hasADot) |
| 66 | + return false; |
| 67 | + |
| 68 | + hasADot = 1; |
| 69 | + |
| 70 | + if(hasAnE) |
| 71 | + return false; |
| 72 | + |
| 73 | + if(nextCharacter === undefined) { |
| 74 | + if(currentIndex === 0) |
| 75 | + return false; |
| 76 | + else |
| 77 | + return true; |
| 78 | + } else if(!isNaN(nextCharacter) && nextCharacter !== " ") |
| 79 | + return validateForNumber(currentString, currentIndex + 1); |
| 80 | + else if(nextCharacter === "e") { |
| 81 | + if(currentString[currentIndex - 1] === undefined) |
| 82 | + return false; |
| 83 | + |
| 84 | + return validateForE(currentString, currentIndex + 1); |
| 85 | + } |
| 86 | + else |
| 87 | + return false; |
| 88 | +} |
| 89 | + |
| 90 | +let validateForE = (currentString, currentIndex) => { |
| 91 | + let nextCharacter = currentString[currentIndex + 1]; |
| 92 | + |
| 93 | + if(hasAnE) |
| 94 | + return false; |
| 95 | + |
| 96 | + hasAnE = 1; |
| 97 | + |
| 98 | + if(nextCharacter === "+" || nextCharacter === "-") { |
| 99 | + return validateForSign(currentString, currentIndex + 1); |
| 100 | + } else if(!isNaN(nextCharacter)) { |
| 101 | + return validateForNumber(currentString, currentIndex + 1); |
| 102 | + } else |
| 103 | + return false; |
| 104 | +} |
0 commit comments