-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathho.js
113 lines (104 loc) · 3.04 KB
/
ho.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const inputValue = document.getElementById('inputValue');
const numValue = document.getElementById('numValue');
const strValue = document.getElementById('strValue');
const boolValue = document.getElementById('boolValue');
const result1 = document.getElementById('result1');
const result2 = document.getElementById('result2');
let input
let typeOfInput
/**
* 사용자 입력에 대해서 타입을 검사하고 출력하는 함수
*/
inputValue.addEventListener('submit', (e) => {
e.preventDefault();
input = inputValue[0].value;
typeOfInput = getType(input);
result1.innerText = typeOfInput;
});
numValue.addEventListener('click', (e) => {
e.preventDefault();
switch (typeOfInput) {
case "number":
alert('same type');
break;
case "boolean":
if (input === "true")
result2.innerText = 1;
else
result2.innerText = 0;
result1.innerText = "number";
break;
case "string":
if (isNaN(input))
alert('cannot change');
else {
result2.innerText = input;
result1.innerText = "number";
}
break;
default:
alert('cannot change');
break;
}
});
strValue.addEventListener('click', (e) => {
e.preventDefault();
switch (typeOfInput) {
case "number":
result2.innerText = String(input);
result1.innerText = "string";
break;
case "boolean":
if (input === "true" || input === "false")
result1.innerText = "string";
else
alert("cannot change");
break;
case "string":
alert('same type');
break;
default:
alert('cannot change');
break;
}
});
boolValue.addEventListener('click', (e) => {
e.preventDefault();
switch (typeOfInput) {
case "number":
if (input === "0")
result2.innerText = "false";
else
result2.innerText = "true";
result1.innerText = "boolean";
break;
case "boolean":
alert("same type");
break;
case "string":
if (input === "true" || input === "false")
result1.innerText = "boolean";
else
alert('cannot change');
break;
default:
alert('cannot change');
break;
}
});
/**
* 타입을 반환하는 함수
* @param {*} input : 타입을 알고 싶은 변수
* @returns : 매개변수의 타입 반환
*/
function getType(input) {
if (!isNaN(input)) {
return "number";
} else if (input === "true" || input === "false") {
return "boolean";
} else if (typeof input === "string") {
return "string";
} else {
return "unknown";
}
}