-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (45 loc) · 1.34 KB
/
script.js
File metadata and controls
50 lines (45 loc) · 1.34 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
50
const inputText = document.getElementById("text-input");
const checkButton = document.getElementById("check-btn");
const resultText = document.getElementById("result");
let isPal = true;
function cleanInputString(str) {
//Regex for sanitizing string
//const regex = /[a-z]/g;
const regex = /[A-Za-z0-9]/g;
return str.toLowerCase().match(regex);
}
function isPalindrome() {
const cleanedString = cleanInputString(inputText.value);
//logic for palindrome
console.log(`Checking ${cleanedString} ...`);
for (let i = 0; i < cleanedString.length; i++) {
if (cleanedString[i] !== cleanedString[cleanedString.length - i - 1]) {
isPal = false;
printResult(inputText.value);
console.log("false...")
return;
}
}
console.log("outside for loop")
isPal = true;
printResult(inputText.value);
}
function printResult(str) {
resultText.innerText = "";
let resultTextString = isPal ?
`<strong>${str}</strong> is a palindrome` :
`<strong>${str}</strong> is not a palindrome`;
resultText.innerHTML = `
<p>${resultTextString}</p>
`;
resultText.classList.remove("hidden");
}
function wordCheck() {
if (inputText.value === "") {
alert("Please input a value");
return;
}
isPalindrome();
console.log(`word checked! - ${inputText.value}`);
}
checkButton.addEventListener('click', wordCheck);