-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
33 lines (26 loc) · 1.09 KB
/
script.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
async function fetchData() {
const apiUrl = 'https://api.dictionaryapi.dev/api/v2/entries/en/';
const wordInput = document.getElementById('wordInput');
const word = wordInput.value.trim();
if (!word) {
alert('Please enter a word.');
return;
}
try {
const response = await fetch(`${apiUrl}${word}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
// Assuming the API response has a specific structure
const wordDefinition = data[0]?.meanings[0]?.definitions[0]?.definition;
if (wordDefinition) {
document.getElementById('result').innerText = `Definition of ${word}: ${wordDefinition}`;
} else {
document.getElementById('result').innerText = `No definition found for ${word}.`;
}
} catch (error) {
console.error('Error fetching data:', error);
document.getElementById('result').innerText = 'Error fetching data. Please try again later.';
}
}