-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (67 loc) · 3.1 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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const wordDetails = document.getElementById('wordDetails');
const errorMessage = document.getElementById('errorMessage');
const loading = document.getElementById('loading');
searchInput.addEventListener('focus', function() {
});
async function searchWord() {
const word = searchInput.value.trim();
if (!word) return;
wordDetails.style.display = 'none';
errorMessage.style.display = 'none';
loading.style.display = 'block';
searchButton.disabled = true;
try {
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word.toLowerCase()}`);
if (!response.ok) {
throw new Error('Word not found');
}
const data = await response.json();
displayResults(data[0]);
} catch (error) {
errorMessage.textContent = 'Sorry, we couldn\'t find that word. Please try another one.';
errorMessage.style.display = 'block';
wordDetails.style.display = 'none';
} finally {
loading.style.display = 'none';
searchButton.disabled = false;
}
}
function displayResults(data) {
wordDetails.innerHTML = `
<div class="word-title">
${data.word}
${data.phonetic ? `
<i class="fas fa-volume-up speaker-icon" onclick="playAudio('${data.phonetics[0].audio}')"></i>
` : ''}
</div>
${data.meanings.map(meaning => `
<div class="meaning-section">
<div class="part-of-speech">${meaning.partOfSpeech}</div>
${meaning.definitions.map(def => `
<div class="definition">
<p>${def.definition}</p>
${def.example ? `<p class="example">Example: ${def.example}</p>` : ''}
${def.synonyms.length > 0 ? `
<p class="synonyms">Synonyms: ${def.synonyms.join(', ')}</p>
` : ''}
</div>
`).join('')}
</div>
`).join('')}
${data.origin ? `
<div class="meaning-section">
<div class="part-of-speech">Origin</div>
<div class="definition">
<p>${data.origin}</p>
</div>
</div>
` : ''}
`;
wordDetails.style.display = 'block';
}
function playAudio(audioUrl) {
const audio = new Audio(audioUrl);
audio.play();
}