56 lines
1.7 KiB
HTML
Executable File
56 lines
1.7 KiB
HTML
Executable File
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Document</title>
|
|
</head>
|
|
<body>
|
|
|
|
|
|
<script>
|
|
var myAtoi = function(str) {
|
|
var newVal = "";
|
|
if (new RegExp("^[ ]+$").test(str)) {
|
|
return 0
|
|
} else {
|
|
if (str.length == 0) {
|
|
return 0
|
|
} else if (str.length == 1 && (str == "+" || str == "-")) {
|
|
return 0
|
|
} else {
|
|
str = str.replace(/(^\s*)|(\s*$)/g, "");
|
|
for (var i = 0; i<str.length; i++) {
|
|
if (i == 0 && ( str[i] == "-" || str[i] == "+" || new RegExp("[0-9]+").test(str[i]) )) {
|
|
newVal += str[i]
|
|
} else if ( str[i] == "-" || str[i] == "+" || str[i] == ".") {
|
|
break
|
|
} else if (new RegExp("[0-9]+").test(str[i])) {
|
|
newVal += str[i]
|
|
} else if (i == 0 && /^[a-zA-Z]+$/.test(str[i])){
|
|
return 0
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (newVal > 2147483648 - 1) {
|
|
newVal = 2147483647
|
|
} else if (newVal < -2147483648) {
|
|
newVal = -2147483648
|
|
}
|
|
return newVal
|
|
|
|
};
|
|
|
|
// 空 "" 0
|
|
// 空格 " " 0
|
|
// +
|
|
console.log(myAtoi("+-42"))
|
|
// myAtoi("-135")
|
|
</script>
|
|
</body>
|
|
</html> |