-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (52 loc) · 2.07 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
const url = ` https://api.dictionaryapi.dev/api/v2/entries/en/`;
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementsByClassName('search-button')[0];
const wordLabel = document.getElementsByClassName('word')[0];
const wordInfo = document.getElementsByClassName('word-info')[0];
const definitionLabel = document.getElementsByClassName('definition')[0];
const exampleLabel = document.getElementsByClassName('example')[0];
const volumeButton = document.getElementsByClassName('volume-button')[0];
const sound = document.getElementById('sound');
const volumeStatus = document.getElementsByClassName('volume-status')[0];
let wordAvailable = true;
function fetchApi() {
fetch(`${url}${searchInput.value}`)
.then((response) => response.json())
.then(
(data) => {
try {
console.log(data)
wordLabel.textContent = data[0].word;
wordInfo.textContent = `${data[0].meanings[0].partOfSpeech} /${data[0].phonetic}/`;
definitionLabel.textContent = data[0].meanings[0].definitions[0].definition;
exampleLabel.textContent = data[0].meanings[0].definitions[0].example || 'No examples available for this word';
sound.setAttribute('src', `${data[0].phonetics[0].audio}`);
} catch(error) {
wordAvailable = false;
wordLabel.textContent = 'Unavailable';
}
})
}
searchButton.addEventListener('click', function() {
fetchApi();
})
volumeButton.addEventListener('click', function() {
if (sound.getAttribute('src') !== '') {
sound.play();
}
})
searchInput.addEventListener('keyup', function(event) {
event.preventDefault();
if (event.keyCode === 13) {
fetchApi();
}
})
volumeButton.addEventListener('mouseover', function() {
volumeStatus.style.width = '15vh';
if (wordAvailable & sound.getAttribute('src') !== '') {volumeStatus.textContent = 'Hear the pronounciation';}
else {volumeStatus.textContent = 'Unavailable for this word'}
})
volumeButton.addEventListener('mouseout', function() {
volumeStatus.style.width = 0;
volumeStatus.textContent = '';
})