-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (43 loc) · 1.55 KB
/
Copy pathscript.js
File metadata and controls
55 lines (43 loc) · 1.55 KB
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
let inputText = document.getElementById("inputText");
let searchBtn = document.getElementById("searchBtn");
let result = document.querySelector(".meaning-container");
const getWordInfo = async (query) => {
try {
let response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${query}`);
let data1 =await response.json();
console.log(data1)
let definations = data1[0].meanings[0].definitions[0];
result.innerHTML = `
<h2> <strong>Word:</strong>${data1[0].word}</h2>
<p> ${data1[0].meanings[0].partOfSpeech}</p>
<p><strong>Meaning:</strong> ${definations.definition== undefined? "Not Found": definations.definition}</p>
<p><strong>Example:</strong> ${definations.example== undefined? "Not Found" : definations.example}</p>
<p><strong>Antonym:</strong> </p>
`;
if(definations.antonyms !=""){
for(let i =0 ; i<definations.antonyms.length ;i++)
{
result.innerHTML = result.innerHTML + `<li> ${definations.antonyms[i]} </li>`
}
}
else{
result.innerHTML = result.innerHTML + `<span> Not Found</span>`
}
//Adding Read More Button
result.innerHTML = result.innerHTML + `<div><a href="${data1[0].sourceUrls}" target="_blank">Read More</a></div> `;
}
catch (error) {
result.innerHTML = `<p> Sorry the word could not be found</p>`
}
}
searchBtn.addEventListener('click',(e) => {
let data = inputText.value.trim();
if(data =="")
{
alert("enter something");
}
else
{
getWordInfo(data);
}
});