-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
146 lines (124 loc) · 3.96 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
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
138
139
140
141
142
143
144
145
146
const startBtn = document.getElementById('start-app');
const wordItem = document.getElementById('word-item');
const promptWord = document.getElementById('prompt-word');
const form = document.getElementById('word-form');
const formInput = document.getElementById('word-input');
const solutionContainer = document.getElementById('solution-container');
const submitedWord = document.getElementById('submited-word');
const solutionMessage = document.getElementById('solution-message');
const solutionBtn = document.getElementById('solution-button');
const solutionWrapper = document.getElementById('solution-box');
const solutionList = document.getElementById('solution-list');
const restartBtn = document.getElementById('restart-button');
function getRandomWord(arr) {
const randomIndex = Math.floor(Math.random() * arr.length + 1);
const randomWord = arr[randomIndex].word;
const allSynonyms = arr
.filter((obj) => obj.word === randomWord)
.map((obj) => obj.synonyms)
.flat()
.map((word) => word.toLowerCase());
const synonymArray = [...new Set(allSynonyms)];
const outputWord = {
word: randomWord,
synonyms: synonymArray,
};
return outputWord;
}
async function getData() {
try {
const res = await fetch('./dataset/thesaurus.json');
if (!res.ok) {
throw new Error('Something went wrong');
}
const data = await res.json();
//call getRandomWord on data
const randomWordObject = getRandomWord(data);
promptWord.textContent = randomWordObject.word;
// Create a label element for each element in the synonyms array
randomWordObject.synonyms.forEach((synonym) => {
const li = document.createElement('li');
li.textContent = synonym;
solutionList.appendChild(li);
});
show(startBtn);
} catch (error) {
showErrorMessage();
console.error('Something went wrong');
}
}
function onStart() {
show(wordItem);
show(promptWord);
disable(startBtn);
startBtn.style.opacity = '0';
startBtn.style.cursor = 'auto';
}
function onSubmit(e) {
e.preventDefault();
const input = formInput.value;
if (input.trim() === '') {
alert('Type a synonym');
return;
}
const array = Array.from(document.querySelectorAll('li')).map(
(li) => li.textContent
);
if (array.includes(input.trim().toLowerCase())) {
solutionMessage.innerHTML = `<span style="color:green" aria-label="Correct">🟢</span> Correct!`;
} else {
solutionMessage.innerHTML = `<span style="color:red" aria-label="Not a synonym">🔴</span> That doesn't seem to be a synonym`;
}
submitedWord.textContent = input;
formInput.value = '';
show(solutionContainer);
disable(formInput);
}
function onRestart() {
getData();
show(promptWord);
enable(formInput);
hide(solutionContainer);
hide(solutionWrapper);
solutionBtn.textContent = 'Show solution';
document.querySelectorAll('li').forEach((li) => li.remove());
}
function toggleSolution() {
if (solutionBtn.textContent === 'Hide solution') {
hide(solutionWrapper);
solutionBtn.textContent = 'Show solution';
} else {
show(solutionWrapper);
solutionBtn.textContent = 'Hide solution';
}
}
function showErrorMessage() {
const errorMsg = document.createElement('h2');
errorMsg.textContent = 'Oops... something went wrong';
errorMsg.style.marginTop = '4rem';
document.querySelector('header').insertAdjacentElement('afterend', errorMsg);
}
function show(item) {
if (item.classList.contains('hidden')) {
item.classList.remove('hidden');
}
}
function hide(item) {
if (!item.classList.contains('hidden')) {
item.classList.add('hidden');
}
}
function disable(item) {
item.disabled = true;
}
function enable(item) {
item.disabled = false;
}
function init() {
startBtn.addEventListener('click', onStart);
form.addEventListener('submit', onSubmit);
solutionBtn.addEventListener('click', toggleSolution);
restartBtn.addEventListener('click', onRestart);
document.addEventListener('DOMContentLoaded', getData);
}
init();