-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
37 lines (32 loc) · 1.05 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
const text = document.getElementById("input");
const search = document.getElementById("search");
const apiKey = "be925f32-b992-41b3-a3fb-57036e339bd3";
const result = document.getElementById("results");
const loading = document.getElementById("loading");
const notFound = document.getElementById('not__found');
// Return API String
function returnApiKey(keyword) {
const apivariable = `https://www.dictionaryapi.com/api/v3/references/collegiate/json/${keyword}?key=${apiKey}`;
return apivariable;
}
// Adding eventlistener to search
search.addEventListener("click", async () => {
const keyord = text.value;
const apiString = returnApiKey(keyord);
result.innerHTML = "";
loading.style.display = "block";
const data = await fetch(apiString)
.then((res) => res.json())
.then((data) => {
return data[0];
});
// console.log(data);
if (typeof data[0] === 'string') {
console.log(data);
loading.innerText = "No such word";
return;
} else {
loading.style.display = "none";
result.innerText = data.shortdef[0];
}
});