-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringToInteger.ts
More file actions
49 lines (46 loc) · 1.17 KB
/
stringToInteger.ts
File metadata and controls
49 lines (46 loc) · 1.17 KB
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
function myAtoi(s: string): number {
let a = s.split("");
let res = "";
for (let i = 0; i < a.length; i++) {
let c = a[i];
if (c >= "0" && c <= "9") {
// it is a number
res += c;
} else if (c === "-" || c === "+") {
// it is a minus
if (res) break;
res += c;
} else if (c === " ") {
// it is a space
if (res) break;
} else {
// it is something else
break;
}
}
let resnum = Number(res);
if (Number.isNaN(resnum)) {
resnum = 0;
}
if (resnum > 2147483647) {
resnum = 2147483647;
} else if (resnum < -2147483648) {
resnum = -2147483648;
}
return resnum;
}
// Test
import { expect } from "chai";
describe("8. String to Integer (atoi)", () => {
it("can atoi", () => {
expect(myAtoi("42")).to.equal(42);
expect(myAtoi(" -42")).to.equal(-42);
expect(myAtoi("4193 with words")).to.equal(4193);
expect(myAtoi("words and 987")).to.equal(0);
expect(myAtoi("-91283472332")).to.equal(-2147483648);
expect(myAtoi("3.14159")).to.equal(3);
expect(myAtoi("-+12")).to.equal(0);
expect(myAtoi("+1")).to.equal(1);
expect(myAtoi("-5-")).to.equal(-5);
});
});