-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (115 loc) · 4.17 KB
/
app.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
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
(function () {
const searchInput = document.querySelector('.search');
const form = document.querySelector('.search-form');
const incorrectDiv = document.querySelector('.incorrect');
const statesList = document.querySelector('.state-list');
const scoreHtml = document.querySelector('.score');
const audio = document.getElementsByTagName('audio');
const revealAll = document.querySelector('.js-reveal-all');
const clear = document.querySelector('.js-clear');
const targetScore = document.querySelector('.js-target-score');
let correctGuesses = localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')) : [];
localStorage.setItem('items', JSON.stringify(correctGuesses));
const data = JSON.parse(localStorage.getItem('items'));
let currentScore = data.length;
scoreHtml.innerHTML = currentScore;
// fetch the json file
fetch('./states.json').then((response) => {
// Convert to JSON
return response.json();
}).then((data) => {
window.states = data;
targetScore.innerHTML = states.length;
searchInput.focus();
});
// create li and add to ul
const liMaker = (item, customClass = 'correct') => {
const li = document.createElement('li');
li.classList.add(customClass);
li.textContent = item;
statesList.appendChild(li);
}
// create list items for the items in localstorage
data.forEach(item => {
liMaker(item);
});
// Clear localStorage
clear.addEventListener('click', () => {
localStorage.clear();
while (statesList.firstChild) {
statesList.removeChild(statesList.firstChild);
};
scoreHtml.innerHTML = 0;
location.reload();
});
// Update the score
function updateScore(){
currentScore ++;
if(currentScore === states.length){
audio[0].play();
}
scoreHtml.classList.add('grow');
scoreHtml.innerHTML = currentScore;
scoreHtml.addEventListener('transitionend', () => {
scoreHtml.classList.remove('grow');
})
};
// Update localstorage with the correct guess
function updateStorage(guess) {
correctGuesses.push(guess);
localStorage.setItem('items', JSON.stringify(correctGuesses));
const data = localStorage.getItem('items');
}
// Capture the submit event
form.addEventListener('submit',(e) => {
const guess = capitaliseString(searchInput.value);
let match = states.find(state => state.name === guess);
// Incorrect guess
if (match == null) {
incorrectDiv.classList.add('show');
setTimeout(() => {
incorrectDiv.classList.remove('show');
}, 1000)
e.preventDefault();
searchInput.value='';
return;
}
// Already guessed
if (correctGuesses.indexOf(guess) > -1) {
statesList.childNodes.forEach((item) => {
if(item.innerHTML === guess) {
item.classList.add('is-already-visible');
setTimeout(() => {
item.classList.remove('is-already-visible');
}, 250);
}
});
e.preventDefault();
searchInput.value='';
return;
}
// Correct
liMaker(guess);
updateScore();
updateStorage(guess);
searchInput.value='';
e.preventDefault();
});
// Capitalise a string
function capitaliseString(string, foo){
return string.replace(/\b\w/g, l => l.toUpperCase());
}
// Reveal the remaining
revealAll.addEventListener('click', function(){
// convert remaining states to an array
let remainingStatesArr = states.map(state => state.name);
// remove correct items from the remaining array
let remainingStates = remainingStatesArr.filter(compareArrays);
function compareArrays(el){
return correctGuesses.indexOf(el) < 0;
}
remainingStates.forEach(item => {
liMaker(item, 'correct-not-guessed');
});
});
})();