-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (94 loc) · 3.63 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let wordElement = document.querySelector(".word");
let definitionElement = document.querySelector(".definition");
let posElement = document.querySelector(".partOfSpeech");
let pronounciationElement = document.querySelector(".pronounciation");
let button = document.querySelector(".newWord");
let APIKEY = "YOURAPIKEY";
if (APIKEY = "YOURAPIKEY"){
alert("Hey, try adding your wordnik API key")
}
window.addEventListener('DOMContentLoaded', renderUI);
async function renderUI(){
let word = await getWord();
let wordData = await getWordData(word);
console.log("word - ", wordData.word)
console.log("pronounciation - ", wordData.pronounciation)
console.log("part of speech - ", wordData.partOfSpeech)
console.log("definition - ", wordData.definition)
wordElement.textContent = wordData.word;
definitionElement.textContent = wordData.definition;
posElement.textContent = wordData.partOfSpeech;
pronounciationElement.textContent = wordData.pronounciation;
}
async function getWord(){
let response;
document.querySelector(".alert").textContent = ""
try{
response = await fetch(`https://api.wordnik.com/v4/words.json/randomWord?hasDictionaryDef=true&includePartOfSpeech=noun%2C%20adjective%2C%20verb%2C%20adverb%2C%20interjection%2C%20pronoun%2C%20preposition%2C%20affix%2C%20verb-intransitive%2C%20verb-transitive&minLength=5&minCorpusCount=1000&api_key=${APIKEY}`);
if (response.status != 200){
throw new Error (response.status)
}
let wordData = await response.json();
console.log(wordData.word)
return wordData.word;
}catch(err){
if (err == 429){
console.log("slow down")
document.querySelector(".alert").textContent = "Slow down bud"
setTimeout(getWord(), 3000);
}
}
}
async function getWordData(word){
let pronounciation;
let partOfSpeech;
let definition;
try{
let response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
if (response.status != 200){
throw new Error (response.status)
}
let definitionData = await response.json();
if(definitionData.title || definitionData[0].meanings.length == 0){
return {};
}else{
definition = definitionData[0].meanings[0].definitions[0].definition;
partOfSpeech = definitionData[0].meanings[0].partOfSpeech;
if(definitionData[0].phonetics.length == 0 ){
pronounciation = "";
}else{
pronounciation = definitionData[0].phonetics[0].text;
}
return {
word: word,
definition: definition,
partOfSpeech: partOfSpeech,
pronounciation: pronounciation
}
}
}catch(err){
console.log("ERROR",err);
}
}
button.addEventListener("click", renderUI);
//---------------------------PROBLEMS TO ACCOUNT FOR
//no pronounciation of word (when the phonetics array is empty)
//no meaning (but he word is still present see below - the meaning array is empty)
// [
// {
// "word": "coquettishness",
// "phonetics": [
// {
// "text": "/koʊˈkɛdɪʃnəs/",
// "audio": "https://lex-audio.useremarkable.com/mp3/coquettishness_us_1.mp3"
// }
// ],
// "meanings": []
// }
// ]
//word not found
// {
// "title": "No Definitions Found",
// "message": "Sorry pal, we couldn't find definitions for the word you were looking for.",
// "resolution": "You can try the search again at later time or head to the web instead."
// }