File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ const  url  =  "https://api.thecatapi.com/v1/images/search" ; 
2+ const  section  =  document . querySelector ( ".container" ) ; 
3+ const  button  =  document . querySelector ( ".btn" ) ; 
4+ 
5+ button . addEventListener ( "click" ,  getRandomCats ) ; 
6+ 
7+ randomCatPhoto  =  ( json )  =>  { 
8+   let  photo  =  json [ 0 ] . url ; 
9+   section . classList . add ( "cats" ) ; 
10+ 
11+   let  image  =  document . createElement ( "img" ) ; 
12+   image . src  =  photo ; 
13+   image . classList . add ( "random_cats" ) ; 
14+   image . alt  =  photo ; 
15+   section . appendChild ( image ) ; 
16+ } ; 
17+ 
18+ async  function  getRandomCats ( )  { 
19+   section . innerHTML  =  "" ; 
20+   try  { 
21+     const  response  =  await  fetch ( url ) ; 
22+     const  json  =  await  response . json ( ) ; 
23+     console . log ( "JSON:" ,  json ) ; 
24+     return  randomCatPhoto ( json ) ; 
25+   }  catch  ( e )  { 
26+     console . log ( "This is an error" ) ; 
27+     console . log ( e ) ; 
28+   } 
29+ } 
Original file line number Diff line number Diff line change 1+ < html  lang ="en "> 
2+   < head > 
3+     < meta  charset ="UTF-8 " /> 
4+     < meta  http-equiv ="X-UA-Compatible " content ="IE=edge " /> 
5+     < meta  name ="viewport " content ="width=device-width, initial-scale=1.0 " /> 
6+     < title > Random Cat Images</ title > 
7+     < link  rel ="stylesheet " href ="style.css " /> 
8+   </ head > 
9+   < body > 
10+     < section  class ="header-content "> 
11+       < button  class ="btn "> Get Random Cat</ button > 
12+     </ section > 
13+ 
14+     < div  class ="container "> </ div > 
15+ 
16+     < script  src ="app.js "> </ script > 
17+   </ body > 
18+ </ html > 
Original file line number Diff line number Diff line change 1+ body  {
2+   background :  rgb (17 ,  17 ,  17 );
3+ }
4+ 
5+ .header-content  {
6+   text-align :  center;
7+ }
8+ 
9+ .btn  {
10+   background :  transparent;
11+   border :  none;
12+   background-color :  rgb (216 ,  34 ,  74 );
13+   color :  # fff
14+   padding :  10px   20px  ;
15+   cursor :  pointer;
16+ }
17+ 
18+ /* JavaScript */ 
19+ .cats  {
20+   display :  flex;
21+   justify-content :  center;
22+   align-items :  center;
23+   height :  100%  ;
24+ }
25+ 
26+ .random_cats  {
27+   width :  40%  ;
28+ }
29+ 
30+ img  {
31+   max-width :  700px  ;
32+   height :  30rem  ;
33+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments