1- const wrapper = document . querySelector ( ".wrapper" ) ,
2- searchInput = wrapper . querySelector ( "input" ) ,
3- synonyms = wrapper . querySelector ( ".synonyms .list" ) ,
4- infoText = wrapper . querySelector ( ".info-text" ) ,
5- volumeIcon = wrapper . querySelector ( ".word i" ) ,
6- removeIcon = wrapper . querySelector ( ".search span" ) ;
7- let audio ;
8-
9-
10- function data ( result , word ) {
11- if ( result . title ) {
12- infoText . innerHTML = `Can't find the meaning of <span>"${ word } "</span>.Please try to search for another
13- word` ;
14- }
15- else {
16- console . log ( result ) ;
17- wrapper . classList . add ( "active" ) ;
18- let definitions = result [ 0 ] . meanings [ 0 ] . definitions [ 0 ] ,
19- phonetics = `${ result [ 0 ] . meanings [ 0 ] . partOfSpeech } / ${ result [ 0 ] . phonetics [ 0 ] . text } / ` ;
20-
21- document . querySelector ( '.word p' ) . innerText = result [ 0 ] . word ;
22- document . querySelector ( '.word span' ) . innerText = phonetics ;
23- document . querySelector ( '.meaning span' ) . innerText = definitions . definition ;
24- document . querySelector ( '.example span' ) . innerText = definitions . example ;
25- audio = new Audio ( 'https:' + result [ 0 ] . phonetics [ 0 ] . audio ) ;
26-
27- if ( definitions . synonyms [ 0 ] == undefined ) {
28- synonyms . parentElement . style . display = 'none' ;
29- }
30- else {
31- synonyms . parentElement . style . display = 'block' ;
32- synonyms . innerHTML = "" ;
33- for ( let i = 0 ; i < 5 ; i ++ ) {
34- let tag = `<span onclick = search('${ definitions . synonyms [ i ] } ')>${ definitions . synonyms [ i ] } ,</span>` ;
35- synonyms . insertAdjacentHTML ( 'beforeend' , tag ) ;
36- }
37- }
1+ const containerEl = document . getElementById ( "container" ) ,
2+ searchInput = document . getElementById ( "input" ) ;
3+ infoTextEl = document . getElementById ( "info-text" ) ;
4+ const audioEl = document . getElementById ( "audio" ) ;
5+
6+ const meaningContainerEl = document . getElementById ( "meaning-container" ) ;
7+
8+ const titleEl = document . getElementById ( "title" ) ;
9+ const meaningEl = document . getElementById ( "meaning" ) ;
10+
11+ async function fetchApi ( word ) {
12+ try {
13+ infoTextEl . style . display = "block" ;
14+ infoTextEl . innerText = `Searching the meaning of "${ word } "` ;
15+ meaningContainerEl . style . display = "none" ;
16+ const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${ word } ` ;
17+ const result = await fetch ( url ) . then ( ( res ) => res . json ( ) ) ;
18+
19+ if ( result . title ) {
20+ titleEl . innerText = word ;
21+ meaningEl . innerText = "N/A" ;
22+ audioEl . style . display = "none" ;
3823 }
39- }
4024
41- function search ( word ) {
42- searchInput . value = word ;
43- fetchApi ( word ) ;
44- }
25+ const definitions = result [ 0 ] . meanings [ 0 ] . definitions [ 0 ] ;
4526
46- function fetchApi ( word ) {
47- wrapper . classList . remove ( 'active' ) ;
48- infoText . style . color = '#000' ;
49- infoText . innerHTML = `Searching the meaning of<span>"${ word } "</span>` ;
50- let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${ word } ` ;
51- // fetching api response and returning it with parsing into js obj
52- //method calling data with passing api response
53- fetch ( url ) . then ( res => res . json ( ) ) . then ( result => data ( result , word ) ) ;
54- }
27+ titleEl . innerText = result [ 0 ] . word ;
5528
29+ meaningEl . innerText = definitions . definition ;
5630
57- searchInput . addEventListener ( 'keyup' , e => {
58- if ( e . key === 'Enter' && e . target . value ) {
59- fetchApi ( e . target . value ) ;
31+ if ( result [ 0 ] . phonetics [ 0 ] . audio ) {
32+ audioEl . style . display = "inline-flex" ;
33+
34+ audioEl . src = result [ 0 ] . phonetics [ 0 ] . audio ;
35+ } else {
36+ audioEl . style . display = "none" ;
6037 }
61- } ) ;
6238
63- volumeIcon . addEventListener ( 'click' , ( ) => {
64- audio . play ( ) ;
65- } ) ;
39+ infoTextEl . style . display = "none" ;
40+ meaningContainerEl . style . display = "block" ;
41+ } catch ( error ) {
42+ console . log ( error ) ;
43+ infoTextEl . style . display = "none" ;
44+ meaningContainerEl . style . display = "block" ;
45+ audioEl . style . display = "none" ;
46+ }
47+ }
6648
67- removeIcon . addEventListener ( 'click' , ( ) => {
68- searchInput . value = "" ;
69- searchInput . focus ( ) ;
70- wrapper . classList . remove ( "active" ) ;
71- infoText . style . color = '#9a9a9a' ;
72- infoText . innerHTML = "Type a word and press enter" ;
73- } ) ;
49+ searchInput . addEventListener ( "keyup" , ( e ) => {
50+ if ( e . key === "Enter" && e . target . value ) {
51+ fetchApi ( e . target . value ) ;
52+ }
53+ } ) ;
0 commit comments