Skip to content

Commit e7e9d3e

Browse files
committed
add logic and add css styling
1 parent 5dc220d commit e7e9d3e

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

css/style.css

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ main form input[type='text']{
3232
flex-grow: 1;
3333

3434
}
35-
main form input,button{
35+
main form input,button,.result div a{
3636
border: none;
3737
font-size: 16px;
3838
font-weight: 500;
@@ -53,4 +53,28 @@ footer{
5353
font-size: 18px;
5454
position: fixed;
5555
width: 100%;
56+
}
57+
58+
.result{
59+
background-color: #fff;
60+
padding: 20px;
61+
border-radius: 10px;
62+
margin-top: 20px;
63+
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
64+
}
65+
.result p{
66+
margin-top: 10px;
67+
}
68+
.result .partsOfSpeech{
69+
font-style: italic;
70+
color: #008080;
71+
margin-top: 2px;
72+
}
73+
.result div {
74+
margin-top: 20px;
75+
}
76+
.result div a{
77+
text-decoration: none;
78+
background-color: #008080;
79+
color: #fff;
5680
}

js/script.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// select elements
44

5-
const form = document.querySelector('form');
5+
let form = document.querySelector('form');
66

7-
const word = document.querySelector('#word');
7+
let word = document.querySelector('#word');
88

9-
const result = document.querySelector('.result');
9+
let result = document.querySelector('.result');
1010

1111
form.addEventListener('submit',(e)=>{
1212
e.preventDefault();
@@ -16,11 +16,57 @@ form.addEventListener('submit',(e)=>{
1616
})
1717

1818
const getData=async(word)=>{
19+
20+
try{
1921
const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
2022
const data = await res.json();
21-
// console.log(data);
23+
console.log(data);
24+
25+
let definitions = data[0].meanings[0].definitions[0];
26+
27+
result.innerHTML = `<h2><strong>Word: </strong>${data[0].word}</h2>
28+
<p class="partsOfSpeech">${data[0].meanings[0].partOfSpeech}</p>
29+
<p><strong>Meaning: </strong>${definitions.definition===undefined?"Not Found":definitions.definition}</p>
30+
<p><strong>Example: </strong>${definitions.example===undefined?"Not Found":definitions.definition}</p>
31+
<p><strong>Antonyms: </strong></p>
32+
`;
33+
34+
if(definitions.antonyms.length ===0){
35+
result.innerHTML +=`<span>Not Found</span> <br>`;
36+
}
37+
else{
38+
for(let i=0;i<definitions.antonyms.length;i++){
39+
result +=`<li>${definitions.antonyms[i]}</li>`
40+
}
41+
42+
43+
}
44+
45+
if(data[0].phonetics.length===0){
46+
result.innerHTML +=`<p>Audio Not Found</p>`;
47+
}
48+
else{
49+
for(let i=0;i<data[0].phonetics.length;i++){
50+
result.innerHTML +=`Audio : <audio controls>
51+
<source src=${data[0].phonetics[i].audio} type="audio/ogg">
52+
53+
Your browser does not support the audio tag.
54+
</audio>`
55+
}
56+
}
2257

23-
result.innerHTML = `<h2>${data[0].meanings[0].definitions[0].definition}</h2>`;
58+
59+
60+
61+
62+
result.innerHTML+= `<div><a href=${data[0].sourceUrls} target="_blank">Read More</a></div>`
63+
64+
}
65+
catch(e){
66+
// console.log(e.message);
67+
result.innerHTML = "<p>Sorry, the word could not be found</p>"
68+
}
2469
// console.log('word' + word);
2570

71+
2672
}

0 commit comments

Comments
 (0)