-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.html
More file actions
137 lines (115 loc) · 4.38 KB
/
palindrome.html
File metadata and controls
137 lines (115 loc) · 4.38 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<title>Palindrome</title>
<style>
body, html {
text-align: center;
margin: 0 auto
background: #0F2027;
background: -webkit-linear-gradient(to right, #2C5364, #203A43, #0F2027);
background: linear-gradient(to right, #2C5364, #203A43, #0F2027);
color: white;
}
p, label {
font-family: sans-serif;
}
h1 {
font-variant: small-caps;
}
.container {
height: 100vh;
width: 100vw;
position: relative;
margin: 0 auto;
}
.content {
margin: 0;
position: absolute;
top: 30%;
left: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
#hide:hover {
cursor: pointer;
color: deepskyblue;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<h1>palindrome checker!</h1>
<label style="top: 50%">
Enter a word to check if it is a Palindrome!
</label>
<br>
<input type="text" id="text">
<input type="submit" value="check" onclick="checkPalindrome()">
<br><br>
<label>
Display the last 10 Palindromes in the last 10 minutes
</label>
<br>
<input type="button" value="display" onclick="displayPalindromes()">
<p id="resultText"></p>
<p id="displayPalindromes" hidden></p>
<p style="color: lightblue; text-decoration: underline;" id="hide" hidden>
hide
</p>
</div>
</div>
<script>
const prevPalindromes = [];
const list = document.getElementById('displayPalindromes');
const hide = document.getElementById('hide');
let x = 0;
function checkPalindrome(){
const resultText = document.getElementById('resultText');
const remove = /[\W_]/g
let inputString = document.getElementById('text').value;
let editedString = inputString.toLowerCase().replace(remove, "")
let reversedString = editedString.split("").reverse().join("");
if (editedString === reversedString){
resultText.innerHTML = inputString + " is a palindrome!";
//if there are already 10 palindrome in the array, remove the oldest element before adding the new palindrome
if (x > 9)
prevPalindromes.shift();
prevPalindromes[x] = inputString;
//set a 10 minute timer for each palindrome added to the array
function timer(){
setTimeout(function(){
prevPalindromes.shift();
x--;
}, 600000);
}
timer();
//Update the list if it is already being displayed
if (list.hidden == false){
displayPalindromes();
}
x++;
}
else
resultText.innerHTML= inputString + " is not a palindrome";
}
function displayPalindromes(){
let i = 0;
let z = "";
list.removeAttribute('hidden');
hide.removeAttribute('hidden');
for (i = 0; i < prevPalindromes.length ; i++){
z = z + prevPalindromes[i] + "<br>";
list.innerHTML = z;
}
}
hide.addEventListener('click', function(){
list.hidden = true;
hide.hidden = true;
});
</script>
</body>
</html>