@@ -128,6 +128,80 @@ const whereAmI = function (lat, lng)
128128 . catch ( err => console . log ( `An Error Ocurred: ${ err } ` ) ) ;
129129} ;
130130
131- whereAmI ( lat1 , lng1 ) ;
131+ // whereAmI(lat1, lng1);
132132// whereAmI(lat2, lng2);
133133// whereAmI(lat3, lng3);
134+
135+ // Coding Challenge 2
136+
137+ /*
138+ Your tasks:
139+ Tasks are not super-descriptive this time, so that you can figure out some stuff by
140+ yourself. Pretend you're working on your own 😉
141+ PART 1
142+ 1. Create a function 'createImage' which receives 'imgPath' as an input.
143+ This function returns a promise which creates a new image (use
144+ document.createElement('img')) and sets the .src attribute to the
145+ provided image path
146+ 2. When the image is done loading, append it to the DOM element with the
147+ 'images' class, and resolve the promise. The fulfilled value should be the
148+ image element itself. In case there is an error loading the image (listen for
149+ the'error' event), reject the promise
150+ 3. If this part is too tricky for you, just watch the first part of the solution
151+ PART 2
152+ 4. Consume the promise using .then and also add an error handler
153+ 5. After the image has loaded, pause execution for 2 seconds using the 'wait'
154+ function we created earlier
155+ 6. After the 2 seconds have passed, hide the current image (set display CSS
156+ property to 'none'), and load a second image (Hint: Use the image element
157+ returned by the 'createImage' promise to hide the current image. You will
158+ need a global variable for that 😉)
159+ 7. After the second image has loaded, pause execution for 2 seconds again
160+ 8. After the 2 seconds have passed, hide the current image
161+ Test data: Images in the img folder. Test the error handler by passing a wrong
162+ image path. Set the network speed to “Fast 3G” in the dev tools Network tab,
163+ otherwise images load too fast
164+ */
165+
166+ let currImg ;
167+
168+ const imgsContainer = document . querySelector ( '.images' ) ;
169+
170+ const hideImg = ( ) =>
171+ {
172+ currImg . style . display = 'none' ;
173+ } ;
174+
175+ const wait = function ( seconds )
176+ {
177+ return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ;
178+ } ;
179+
180+ const createImage = function ( imgPath )
181+ {
182+ return new Promise ( function ( resolve , reject )
183+ {
184+ const img = document . createElement ( 'img' ) ;
185+ img . src = imgPath ;
186+ currImg = img ;
187+
188+ img . addEventListener ( 'load' , function ( )
189+ {
190+ imgsContainer . insertAdjacentElement ( 'afterbegin' , img ) ;
191+ resolve ( img ) ;
192+ } ) ;
193+ img . addEventListener ( 'error' , function ( )
194+ {
195+ reject ( 'Error: Image Not Found' ) ;
196+ } ) ;
197+ } ) ;
198+ } ;
199+
200+ createImage ( 'img/img-1.jpg' )
201+ . then ( ( ) => wait ( 2 ) )
202+ . then ( ( ) => hideImg ( currImg ) )
203+ . then ( ( ) => createImage ( 'img/img-2.jpg' ) )
204+ . then ( ( ) => wait ( 2 ) )
205+ . then ( ( ) => hideImg ( currImg ) )
206+ . then ( ( ) => createImage ( 'img/img-error.jpg' ) )
207+ . catch ( err => console . log ( err ) ) ;
0 commit comments