forked from Taimoormk/plan9-class-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise-Functions-Answers.js
executable file
·97 lines (76 loc) · 1.95 KB
/
Exercise-Functions-Answers.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ########## Exercise 01 ##########
function add(a, b) {
return a + b;
}
console.log(add(1, 2))
Answer: 3
// ########## Exercise 02 ##########
var message = "Outer";
function getMessage() {
return message;
}
function overrideMessage() {
var message = "Inner";
return message;
}
console.log(getMessage())
Answer: Outer
console.log(overrideMessage())
Answer: Inner
console.log(message)
Answer: Inner
// ########## Exercise 03 ##########
var variable = "top-level";
function parentfunction() {
var variable = "local";
function childfunction() {
return variable;
}
return childfunction();
}
console.log(parentfunction())
Answer: local
// ########## Exercise 04 ##########
makeMysteryFunction(makerValue) {
var newFunction = function doMysteriousThing(param) {
return makerValue + param;
};
return newFunction;
};
var mysteryFunction3 = makeMysteryFunction(3);
var mysteryFunction5 = makeMysteryFunction(5);
console.log(mysteryFunction3(10) + mysteryFunction5(5))
Answer:
// ########## Exercise 05 ##########
function returnFirstArg(firstArg) {
return firstArg;
}
console.log(returnFirstArg("first", "second", "third"))
Answer: first
function returnSecondArg(firstArg, secondArg) {
return secondArg;
}
console.log(returnSecondArg("only give first arg"))
Answer: undefined
function returnAllArgs() {
var argsArray = [];
for (var i = 0; i < arguments.length; i += 1) {
argsArray.push(arguments[i]);
}
return argsArray.join(",");
}
console.log(returnAllArgs("first", "second", "third"))
Answer: first, second, third
// ########## Exercise 06 ##########
var appendRules = function(name) {
return name + " rules!";
};
var appendDoubleRules = function(name) {
return name + " totally rules!";
};
var praiseSinger = { givePraise: appendRules };
console.log(praiseSinger.givePraise("John"))
Answer: John rules!
praiseSinger.givePraise = appendDoubleRules;
console.log(praiseSinger.givePraise("Mary"))
Answer: Mary totally rules!