-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
26 lines (23 loc) · 1.02 KB
/
script.js
File metadata and controls
26 lines (23 loc) · 1.02 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
const form = document.querySelector('form');
const result = document.querySelector('.container');
form.addEventListener('submit', (e) => {
e.preventDefault();
getWordinfo(e.target.children[0].value);
})
const getWordinfo = async (word) => {
try{
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await response.json();
const definitions = data[0].meanings[0].definitions[0];
result.innerHTML = `
<h2><strong>${data[0].word.charAt(0).toUpperCase()+ data[0].word.slice(1)}</strong></h2>
<p>${data[0].phonetic===undefined ? "" : data[0].phonetic }</p>
<p>${data[0].meanings[0].partOfSpeech}</p>
<p><strong>Meaning: </strong>${definitions.definition}</p>
<p><strong>Example: </strong>"${definitions.example?definitions.example:"Sorry, no example available"}"</p>
<p><strong>Synonym: </strong>${definitions.synonyms.length===0?"Unavailable":definitions.synonyms}</p>
`;
}catch(exception){
result.innerHTML='<p>Sorry, your word could not be found</p>'
}
}