This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path3-playing-computer.js
More file actions
43 lines (35 loc) · 1.34 KB
/
3-playing-computer.js
File metadata and controls
43 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
You have to predict the output of this program WITHOUT EXECUTING IT.
In order to do this, try writing down the value that all variables take
during each step of the program execution.
Answer the following questions:
1. This program throws an error. Why? (If you can't find it, try executing it). Ans: ReferenceError: b is not defined.
Trying to console out b which is not a defined variable outside the function block.
2. Remove the line that throws the error. Ans: console.log(b) is removed.
3. What is printed to the console? Ans: console prints out 2 6 4 9 6 13 8
4. How many times is "f1" called? Ans: f1 is the else which is 9 and 13 ( twice)
5. How many times is "f2" called? Ans: f2 even numbers are 2 , 6, 4, 6, 8 (5 times)
6. What value does the "a" parameter take in the first "f1" call? Ans: 1st parameter call a = 6
7. What is the value of the "a" outer variable when "f1" is called for the first time? Ans: a + 1 = 7
*/
let x = 2;
let a = 6;
const f1 = function(a, b) {
return a + b;
};
const f2 = function(a, b) {
return a + b + x;
};
console.log(x);
console.log(a);
// console.log(b);
for (let i = 0; i < 5; ++i) {
a = a + 1;
if (i % 2 === 0) {
const d = f2(i, x);
console.log(d);
} else {
const e = f1(i, a);
console.log(e);
}
}