-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromes.js
110 lines (99 loc) · 4.58 KB
/
palindromes.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
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
//javascriptor
//get user input
let notificationBoard = document.querySelector('.notification');
let userSubmitButton = document.querySelector('.userSubmit');
let wordInput = document.querySelector('.wordInput');
userSubmitButton.addEventListener('click', function(){
processInput(wordInput.value);
});
let processInput = (input) => {
let flipWord = (value) => {
//clean remove special characters and punctuations
let newValue='';
value = (value.replace(/([^\w])/g, '*~*')).split('*~*');
for(let i=0; i < value.length; i++){
newValue += value[i]+ "";
}
//flip code
let flippedWord='';
for(let i = 0; i < newValue.length; i++){
flippedWord += (newValue[(newValue.length-i)-1]);
}
if (flippedWord.toLowerCase() === newValue.toLowerCase()) {
addtoLocalStorage((value.join(" ")).toUpperCase());
} else {
listNotification('Sorry, Word or Phrase is not a Palindrome')
}
}
(flipWord(input))
clearDOM();
displayEntry();
}
let listNotification = (message) => {
notificationBoard.textContent=(message)
}
//search list for word/phrase
let findWordExist = (word, wordlist) => {
for(let i=0; i < wordlist.length;i++){
if (wordlist[i].toLowerCase() == word.toLowerCase()){
return true;
}
}
return false;
}
let addtoLocalStorage = (word) => {
let wordlist = localStorage.getItem("wordlist");
let wordlistJSON = JSON.parse(wordlist);
if (wordlistJSON != null){
if ( findWordExist(word, wordlistJSON)){
listNotification('Word or Phrase already in Palindrome entry')
} else {
updateStorage(word, wordlistJSON)
}
} else {
registerStorage(word)
}
}
let registerStorage = (word) => {
let wordlist = [word];
let wordlistJSON = JSON.stringify(wordlist);
localStorage.setItem("wordlist", wordlistJSON);
listNotification('It\'s a Palindrome')
}
let updateStorage = (word, wordlist) => {
if(wordlist.length > 4){
wordlist.pop()
}
wordlist.unshift(word);
let wordlistJSON = JSON.stringify(wordlist);
localStorage.setItem("wordlist", wordlistJSON);
listNotification('It\'s a Palindrome')
}
let displayEntry = () =>{
let wordlist = localStorage.getItem('wordlist');
wordlist = JSON.parse(wordlist);
if(wordlist == null){
return false;
}
for(let i=0; i < wordlist.length; i++){
addToDatalist(wordlist[i]);
}
}
let addToDatalist = (words) =>{
let datalist = document.querySelector('#wordlist');
let list = document.querySelector('.list');
let datalistOptions = document.createElement('option');
datalistOptions.setAttribute('value', words)
datalist.appendChild(datalistOptions);
let listOptions = document.createElement('li');
listOptions.innerHTML = (words)
list.appendChild(listOptions);
}
let clearDOM = () => {
let datalist = document.querySelector('#wordlist');
let list = document.querySelector('.list');
datalist.innerHTML ='';
list.innerHTML ='';
}
clearDOM()
displayEntry();