-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (42 loc) · 1.42 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
const body = document.querySelector('body');
const button = document.querySelector('button');
const wordContainer = document.querySelector('.word-container');
const word = document.createElement('h1');
const definition = document.createElement('p');
body.appendChild(wordContainer);
wordContainer.appendChild(word);
wordContainer.appendChild(definition);
const randomWord = () => {
fetch('https://random-word-api.herokuapp.com/word?number=1')
.then(response => {
return response.json();
})
.then(response => {
word.textContent = response
randomDefinition(word);
wordContainer.classList.remove('display-none');
})
.catch(err => {
console.log(err);
word.textContent = "No Word Available"
});
}
const randomDefinition = (word) => {
fetch(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word.textContent}?key=7e7436ae-9a1a-48e5-a02d-6bfcab41cc8f`)
.then(response => {
return response.json();
})
.then(response => {
console.log(response[0].shortdef[0]);
definition.classList.remove('error');
definition.textContent = "Definition: " + response[0].shortdef[0];
})
.catch(err => {
console.log(err);
definition.classList.add('error');
definition.textContent = "No Definition Available";
});
}
button.addEventListener('click', function(){
randomWord();
})