-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
34 lines (27 loc) · 1.11 KB
/
script.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
/* function palindrome(str) {
const alphanumericOnly = str
// 1) Lowercase the input
.toLowerCase()
// 2) Strip out non-alphanumeric characters
.match(/[a-z0-9]/g);
// 3) return string === reversedString
return alphanumericOnly.join('') ===
alphanumericOnly.reverse().join('');}
palindrome("eye");
*/
function palindrome() {
let inputEl = document.getElementById('palindrome').value;
let displayText = document.getElementById('display-text');
let pattern = /_*\W*/g;
let str = inputEl.toLowerCase().replace(pattern, '');
let reverseString = str.split('').reverse().join('');
if(str != '')
if (str == reverseString) {
displayText.innerHTML = `<span>'${inputEl}'</span> is a palindrome! (${str})`;
}
else {
displayText.innerHTML = `<span>'${inputEl}'</span> not a palindrome!`;
}
else
displayText.innerHTML = 'Empty word!!'
}