-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (76 loc) · 2.13 KB
/
index.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
console.log("script works");
console.log("가나다라");
const checkBtn = document.getElementById("check-btn");
const textInput = document.getElementById("text-input");
const result = document.getElementById("result")
const lightDark = document.getElementById("light-dark");
const checkHangul = (input) => {
const isHangul = /[ㄱ-ㅎㅏ-ㅣ가-힣]/;
console.log('checkHangul result:', isHangul.test(input));
return isHangul.test(input);
}
const cleanInput = (input) => {
const InvalidChar = /[^a-zA-Z0-9가-힣]/g;
const cleaned = input.toLowerCase().replace(InvalidChar, "").trim()
console.log(cleaned);
return cleaned;
}
const checkPalindrome = (cleanedStr) => {
if (cleanedStr.length == 1){
return true;
}
let idx = 0;
while (idx < cleanedStr.length / 2){
if (cleanedStr[idx] !== cleanedStr[cleanedStr.length - 1 - idx]){
return false;
}
idx++;
}
return true;
}
const processInput = () => {
const input = textInput.value;
console.log('input text', input);
if (!input){
alert("Please input a value");
return;
};
//clean input
const cleanedStr = cleanInput(input);
//edge case: if cleanedStr is empty
//clean both hangul and alphabet
//for hangul, leave only complete characters
//ignore anything outside of complete hangul and alphanumeric
if (input && cleanedStr.length == 0){
alert("Invalid input");
return;
}
//determine if input is a palindrome
//if any amount of hangul is included, output comes in korean
let resultText = "";
const hasHangul = checkHangul(input);
if (checkPalindrome(cleanedStr)){
if (hasHangul){
resultText += `회문입니다`;
}
else {
resultText += " is a palindrome";
}
}
else {
if (hasHangul){
resultText += `회문이 아닙니다`;
}
else {
resultText += " is not a palindrome";
}
}
console.log(resultText);
result.innerText = resultText;
}
checkBtn.addEventListener("click", processInput)
lightDark.addEventListener("click", () => {
document.body.classList.toggle("dark-theme");
document.body.classList.toggle("light-theme");
console.log("light-dark clicked");
})