-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseInt.js
52 lines (39 loc) · 1.53 KB
/
parseInt.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
// Implemant parseInt() method
function my_parseInt(string){
let index = "";
let mySlice = '';
if(typeof string !== "number" && typeof string !== "string"){
return NaN
}else if(typeof string === "string"){
index = string.indexOf(".");
mySlice = string.slice(0,index);
return +mySlice
for(let i = 0; i < string.length; i++){
if(typeof string[0] === "string"){
return +string
}else if( typeof string === "string" && typeof string ==="number" && string[0] === "0" && string[1] === "x" ){
return +string
}else if(typeof string === "string" && typeof string ==="number" && string[0] === "0" && string[1] === 'b'){
return +string
}else if(typeof string === "string" && typeof string ==="number" && string[0] === "0" && string[1] === 'x' && string[2] === "f"){
return +string
}
}
}else {
return +string
}
if(typeof string !== isNaN()){
return NaN
}
}
console.log(my_parseInt(0x771));
console.log(my_parseInt(123+123))
console.log(my_parseInt("123.123"))
console.log(my_parseInt(0xf+0165));
console.log(my_parseInt("123"+"123"));
console.log(my_parseInt(null));
console.log(my_parseInt(NaN));
console.log(my_parseInt(0b1010));
console.log(my_parseInt("a123"));
console.log(my_parseInt("123a"));
console.log(my_parseInt(" 123 "));