-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (40 loc) · 1.59 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
const inputEl = document.getElementById("input");
const infoTextEl = document.getElementById("info-text");
const meaningContainerEl = document.getElementById("meaning-container");
const titleEl = document.getElementById("title");
const meaningEl = document.getElementById("meaning");
const audioEl = document.getElementById("audio");
async function fetchAPI(word){
try {
infoTextEl.style.display = "block";
meaningContainerEl.style.display = "none";
infoTextEl.innerText = `Searching the meaning of "${word}"`
//Request from the API
const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`;
const result = await fetch(url).then((res) => res.json());
if(result.title){
meaningContainerEl.style.display = "block";
infoTextEl.style.display = "none";
titleEl.innerText = word;
meaningEl.innerText = "Word not found";
audioEl.style.display = "none";
}
else {
infoTextEl.style.display = "none";
meaningContainerEl.style.display = "block";
audioEl.style.display = "inline-flex";
titleEl.innerText = result[0].word;
meaningEl.innerText = result[0].meanings[0].definitions[0].definition;
audioEl.src = result[0].phonetics[0].audio;
}
//Error Message
} catch (error) {
console.log(error);
infoTextEl.innerText = `an error just occurred, try again later !`
}
}
inputEl.addEventListener("keyup", (e) =>{
if(e.target.value && e.key === "Enter"){
fetchAPI(e.target.value);
}
});