-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathindex.js
67 lines (42 loc) · 1.63 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
function validatePalin() {
string = document.getElementById("text").value;
// to obtain total length of the word or sentence
const len = string.length;
// to remove spaces from the accepted string, if there is any
let string1 = string.replace(/ /g, "");
let len1 = string1.length;
let string2 = "";
// to remove special characters
for (let j = 0; j < len1; j++) {
if((string1[j] >= 'a' && string1[j] <= 'z') || (string1[j] >= 'A' && string1[j] <= 'Z') || (string1[j] >= '0' && string1[j] <= '9')) {
string2 = string2.concat(string1[j]);
}
}
// to obtain total length of the new string
let len2 = string2.length;
// to check for empty string
if (string2 == "") {
document.getElementById("result").innerHTML = "Please enter a valid string!";
}
else {
string2 = string2.toLowerCase();
let flag = 1;
// for loop to divide the words into 2 half
for (let i = 0; i < (len2 / 2); i++) {
// condition to check whether the first and last characters are same
if (string2[i] !== string2[len2 - 1 - i]) {
flag = 0;
}
}
if (flag == 0) {
document.getElementById("result").innerHTML = "It is not a palindrome.";
}
else if (flag == 1) {
document.getElementById("result").innerHTML = "It is a palindrome.";
}
}
}
function clearScreen() {
document.getElementById("result").innerHTML = "";
document.getElementById("text").value = "";
}