Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b29d8b5

Browse files
r0hitsharmabenna100
authored andcommitted
Nomenclature and indentation fixes.
1 parent 28b8616 commit b29d8b5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

JavaScript2/Week5/classwork/extra_examples.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const btn1 = document.getElementById("btn1");
1212
const btn2 = document.getElementById("btn2");
1313

1414
function listenForClick(e) {
15-
// e refers to the click event, and e.target to the elment clicked
16-
console.log(e.target.id);
15+
// e refers to the click event, and e.target to the elment clicked
16+
console.log(e.target.id);
1717
}
1818

1919
btn1.addEventListener("click", listenForClick);
@@ -30,19 +30,19 @@ function listenForClick (e) {
3030
console.log(e.target.id);
3131
// Check which button was clicked, and track it
3232
if(e.target.id === 'btn1')
33-
btn1Clicked = true;
33+
btn1Clicked = true;
3434
else
35-
btn2Clicked = true;
36-
35+
btn2Clicked = true;
36+
3737
// reset both to false after a second
38-
setTimeout(() => {
39-
btn1Clicked = false;
38+
setTimeout(() => {
39+
btn1Clicked = false;
4040
btn2Clicked = false;
4141
}, 1000)
4242

4343
// If both have been tracked as true in the past 1 sec
4444
if(btn1Clicked && btn2Clicked)
45-
console.log("--success--");
45+
console.log("--success--");
4646
}
4747
```
4848

@@ -83,11 +83,11 @@ const id = setInterval(() => {
8383
That works well. But what if we want to apply this sort of animation again, for different colors? It would make sense to turn it into a function which we can call again:
8484

8585
```javascript
86-
function applyForEach(color, delay){
86+
function applyBackgroundForEach(bgColor, delay){
8787
const listItems = document.getElementById("list").children;
8888
let i=0;
8989
const id = setInterval(() => {
90-
listItems[i].style.backgroundColor = color;
90+
listItems[i].style.backgroundColor = bgColor;
9191
i++;
9292

9393
// once we've gone through all the items
@@ -96,14 +96,14 @@ function applyForEach(color, delay){
9696
}, delay);
9797
}
9898

99-
// applyForEach("red", 500);
100-
applyForEach("blue", 300);
99+
// applyBackgroundForEach("red", 500);
100+
applyBackgroundForEach("blue", 300);
101101
```
102102

103103
Our function now lets us switch between which color we want to apply for all the list items. However, it is not flexible enough to let us do other operations on the list items. What if we want to change the `text-decoration` or `font-size`? Or even delete the list items? What if we could run an arbitary operation i.e. `function` on all of the list items:
104104

105105
```javascript
106-
function applyForEach(action, delay){
106+
function applyActionForEach(action, delay){
107107
const listItems = document.getElementById("list").children;
108108
let i=0;
109109
const id = setInterval(() => {
@@ -124,8 +124,8 @@ function makeTextBold(item){
124124
item.style.fontWeight = "bold";
125125
}
126126

127-
// applyForEach(makeBlueBackground, 300);
128-
applyForEach(makeTextBold, 1000);
127+
// applyActionForEach(makeBlueBackground, 300);
128+
applyActionForEach(makeTextBold, 1000);
129129
```
130130

131131
In the above, `applyForEach` now takes an argument `action` which is a `function` that is applied to each of the list items. `action` is therefore a **callback function**. As you can see, callback functions can be both *synchronous* like the one here, or *asynchronous* as the one discussed in class.

0 commit comments

Comments
 (0)