-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (77 loc) · 2.3 KB
/
index.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
const searchBtn = document.getElementById("searchBtn");
const inputWord = document.getElementById("word");
const wordBlock = document.getElementById("wordBlock");
const messageBlock = document.getElementById("messageBlock");
const wordTitle = document.getElementById("wordTitle");
const wordPhonetic = document.getElementById("wordPhonetic");
const wordMeaning = document.getElementById("wordMeaning");
const message = document.getElementById("message")
const searchedWordTemplate = {
word : "",
phonetic : "",
meaning : "",
example : ""
}
const getWord = () => {
return inputWord.value;
}
const getPhonetic = (data) => {
return data[0].phonetic;
}
const getMeaning = (data) => {
return data[0].meanings[0].definitions[0].definition;
}
const updateSearchedWordTemplate = (data) => {
searchedWordTemplate.word = getWord();
searchedWordTemplate.phonetic = getPhonetic(data);
searchedWordTemplate.meaning = getMeaning(data);
}
const updateWordMeaning = () => {
wordMeaning.innerText = searchedWordTemplate.meaning;
}
const updateWordTitle = () => {
wordTitle.innerText = searchedWordTemplate.word;
}
const updateWordPhonetic = () => {
wordPhonetic.innerText = searchedWordTemplate.phonetic;
}
const displayData = (data) => {
updateSearchedWordTemplate(data);
updateWordTitle();
updateWordPhonetic();
updateWordMeaning();
wordBlock.style.display = "block";
}
const updateErrorMessage = (data) => {
message.innerText = data.message;
messageBlock.style.display = "block";
}
const displayErrorMessage = (data) => {
updateErrorMessage(data);
}
const isWordFound = (data) => {
return !(data.message);
}
const resetBlocks = () => {
wordBlock.style.display = "none";
messageBlock.style.display = "none";
}
const searchWord = async () => {
resetBlocks();
try {
const word = getWord();
const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await res.json();
if(isWordFound(data)){
displayData(data);
}else{
displayErrorMessage(data);
}
} catch (error) {
throw new Error("Something went wrong ...");
}
}
searchBtn.addEventListener('click', () => {
console.log("click event")
searchWord();
});