@@ -3610,10 +3610,12 @@ const game = {
3610
3610
/*
3611
3611
3612
3612
This is more of a thinking challenge than a coding challenge 🤓
3613
- Take the IIFE below and at the end of the function, attach an event listener that changes the color of the selected h1 element ('header') to blue,
3613
+ Take the IIFE below and at the end of the function, attach an event listener
3614
+ that changes the color of the selected h1 element ('header') to blue,
3614
3615
each time the BODY element is clicked. Do NOT select the h1 element again!
3615
- And now explain to YOURSELF (or someone around you) WHY this worked! Take all the time you need.
3616
- Think about WHEN exactly the callback function is executed, and what that means for the variables involved in this example.
3616
+ And now explain to YOURSELF WHY this worked! Take all the time you need.
3617
+ Think about WHEN exactly the callback function is executed,
3618
+ and what that means for the variables involved in this example.
3617
3619
3618
3620
GOOD LUCK 😀
3619
3621
@@ -3625,10 +3627,15 @@ const game = {
3625
3627
const header = document . querySelector ( 'h1' ) ;
3626
3628
header . style . color = 'red' ;
3627
3629
document . querySelector ( 'body' ) . addEventListener ( 'click' , function ( ) {
3628
- header . style . color = 'blue' ;
3630
+ const randomColor = Math . floor ( Math . random ( ) * 16777215 ) . toString ( 16 ) ;
3631
+ header . style . color = "#" + randomColor ;
3632
+ console . log ( 'Header color changed to randomly...' ) ;
3629
3633
} ) ;
3630
3634
} ) ( ) ;
3631
3635
3636
+ // When IIFE is executed h1 element on the page becomes red
3637
+ // And at this point the variable environment of IIFE is long gone. (so does the 'const header' variable)
3638
+ // But still, each time when the page is clicked color of the h1 element changes randomly
3632
3639
// This works because of the closure
3633
3640
3634
3641
//#endregion
0 commit comments