-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (37 loc) · 1.53 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
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 manign of "${word}"`
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 = "N/A";
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;
}
} catch (error) {
console.log(error);
infoTextEl.innerText = "An error occured, try again later"
}
}
inputEl.addEventListener("keyup", (e) => {
if (e.target.value && e.key === "Enter") {
fetchAPI(e.target.value);
}
});