Skip to content

Commit fb8f397

Browse files
More on functions chapter examples and exerxises
1 parent 4844a7d commit fb8f397

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

more-on-functions/chapter-examples/anonymous-functions/userInput-is-negative.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let logger = function(errorMsg) {
44
console.log("ERROR: " + errorMsg);
55
};
66
if (userInput < 0) {
7-
// ______("Invalid input");
7+
logger("Invalid input");
88
}
99

1010
// Fill in the blank in line 7 (then uncomment it) so that it logs an error message if userInput is negative.

more-on-functions/chapter-examples/make-function-call-itself/decreasingSum.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ function decreasingSum(integer) {
33
return integer;
44
} else {
55
//call decreasingSum function again
6+
return integer + (decreasingSum(integer-1));
67
}
78
}
89

more-on-functions/exercises/practice-your-skills.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
//Create an anonymous function and set it equal to a variable.
2+
let practice = function(myArg) {
3+
if (typeof myArg === "number") {
4+
return myArg * 3;
5+
} else if (typeof myArg === "string"){
6+
return "ARRR!";
7+
} else {
8+
return myArg;
9+
}
210

11+
}
12+
13+
console.log(practice(5));
314
/* Your function should:
415
a) If passed a number, return the tripled value.
516
b) If passed a string, return the string “ARRR!”

more-on-functions/exercises/raid-a-shuttle.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold
2424
console.log("Fuel level: " + checkFuel(fuelLevel));
2525
console.log("Hold status: " + holdStatus(cargoHold));
2626

27-
/* Steal some fuel from the shuttle:
28-
* /
29-
30-
//a). Define an anonymous function and set it equal to a variable with a normal, non-suspicious name. The function takes one parameter. This will be the fuel level on the shuttle.
3127

32-
//b). You must siphon off fuel without alerting the TAs. Inside your function, you want to reduce the fuel level as much as possible WITHOUT changing the color returned by the checkFuel function.
28+
let nonSuspiciousFunction = function(a) {
29+
if (checkFuel(a) === 'green') {
30+
return a - 100001;
31+
}
32+
else if (checkFuel(a) === 'yellow') {
33+
return a - 50001;
34+
}
35+
else {
36+
return a;
37+
}
38+
}
39+
40+
console.log(nonSuspiciousFunction(3));
3341

34-
//c). Once you figure out how much fuel to pump out, return that value.
3542

36-
//d). Decide where to best place your function call to gather our new fuel.
3743

38-
/* Next, liberate some of that glorious cargo.
39-
* /
4044

4145
//a). Define another anonymous function with an array as a parameter, and set it equal to another innocent variable.
4246

@@ -46,8 +50,7 @@ console.log("Hold status: " + holdStatus(cargoHold));
4650

4751
//d). Don’t get hasty, matey! Remember to test your function.
4852

49-
/* Finally, you need to print a receipt for the accountant. Dont laugh! That genius knows MATH and saves us more gold than you can imagine.
50-
* /
53+
5154

5255
//a). Define a function called irs that can take fuelLevel and cargoHold as arguments.
5356

0 commit comments

Comments
 (0)