Skip to content

Commit dba5a1e

Browse files
committed
Update Udemy_The_Complete_JavaScript_Course_2021.js
1 parent ee08434 commit dba5a1e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Udemy_The_Complete_JavaScript_Course_2021.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,10 +3610,12 @@ const game = {
36103610
/*
36113611
36123612
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,
36143615
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.
36173619
36183620
GOOD LUCK 😀
36193621
@@ -3625,10 +3627,15 @@ const game = {
36253627
const header = document.querySelector('h1');
36263628
header.style.color = 'red';
36273629
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...');
36293633
});
36303634
})();
36313635

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
36323639
// This works because of the closure
36333640

36343641
//#endregion

0 commit comments

Comments
 (0)