-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (48 loc) · 1.46 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
console.log('Extension');
let input=document.querySelector('#input');
let searchBtn=document.querySelector('#search');
let notFound=document.querySelector('.not_found');
let defBox=document.querySelector('.def');
let loading=document.querySelector('.loading');
searchBtn.addEventListener('click',function(e){
e.preventDefault();
//clearData
notFound.innerHTML='';
defBox.innerHTML='';
let word=input.value;
if(word===''){
return;
}
getData(word);
})
async function getData(word){
loading.style.display='block';
const response=await fetch(`https://dictionaryapi.com/api/v3/references/sd3/json/${word}?key=ac054856-6bff-40af-8efb-c28d039b89d6`);
const data=await response.json();
//if empty result
if(!data.length){
loading.style.display='none';
notFound.innerHTML='No result Found';
return;
}
//if result is suggestion
if(typeof data[0]==='string'){
loading.style.display='none';
let heading=document.createElement('h4');
heading.innerHTML='Did you mean?';
notFound.appendChild(heading);
data.forEach(element =>{
let suggestion=document.createElement('span');
suggestion.classList.add('suggested');
suggestion.innerHTML=element;
notFound.appendChild(suggestion);
})
return;
}
//Result Found
loading.style.display='none';
let defination=data[0].shortdef[0];
defBox.innerHTML=defination;
console.log(data)
//Next result
}