Skip to content

Commit bf7c717

Browse files
committed
modified: more-on-functions/exercises/practice-your-skills.js
modified: more-on-functions/exercises/raid-a-shuttle.js
1 parent 4318666 commit bf7c717

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@ b) If passed a string, return the string “ARRR!”
66
c) Be sure to test your function before moving on the next part.
77
*/
88

9-
/* Add to your code! Use your fuction and the map method to change an array as follows:
9+
let anonymous = function (prop) {
10+
if (typeof prop === "number") {
11+
return prop * 3;
12+
} else if (typeof prop === "string") {
13+
return "ARRR!";
14+
}
15+
};
16+
17+
console.log(anonymous("3"));
18+
19+
/* Add to your code! Use your function and the map method to change an array as follows:
1020
a) Triple any the numbers.
1121
b) Replace any strings with “ARRR!”
1222
c) Print the new array to confirm your work.
1323
*/
1424

15-
let arr = ['Elocution', 21, 'Clean teeth', 100];
25+
let anonArr = function (val) {
26+
return val.map((indiv) => {
27+
if (typeof indiv === "number") {
28+
indiv = indiv * 3;
29+
} else if (typeof indiv === "string") {
30+
indiv = "ARRR!";
31+
}
32+
return indiv;
33+
});
34+
};
35+
36+
let arr = ["Elocution", 21, "Clean teeth", 100];
37+
38+
console.log(anonArr(arr));
Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
function checkFuel(level) {
2-
if (level > 100000){
3-
return 'green';
4-
} else if (level > 50000){
5-
return 'yellow';
6-
} else {
7-
return 'red';
8-
}
2+
if (level > 100000) {
3+
return "green";
4+
} else if (level > 50000) {
5+
return "yellow";
6+
} else {
7+
return "red";
8+
}
99
}
1010

11-
function holdStatus(arr){
12-
if (arr.length < 7) {
13-
return `Spaces available: ${7-arr.length}.`;
14-
} else if (arr.length > 7){
15-
return `Over capacity by ${arr.length-7} items.`;
16-
} else {
17-
return "Full";
18-
}
11+
let fuelChecker = function (val) {
12+
if (checkFuel(val) === "green") {
13+
return val - 100001;
14+
} else if (checkFuel(val) === "yellow") {
15+
return val - 50001;
16+
} else return val;
17+
};
18+
19+
function holdStatus(arr) {
20+
if (arr.length < 7) {
21+
return `Spaces available: ${7 - arr.length}.`;
22+
} else if (arr.length > 7) {
23+
return `Over capacity by ${arr.length - 7} items.`;
24+
} else {
25+
return "Full";
26+
}
1927
}
2028

2129
let fuelLevel = 200000;
22-
let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold', 'water', 'AE-35 unit'];
30+
let cargoHold = ["meal kits", "space suits", "first-aid kit", "satellite", "gold", "water", "AE-35 unit"];
31+
fuelChecker(fuelLevel);
2332

2433
console.log("Fuel level: " + checkFuel(fuelLevel));
2534
console.log("Hold status: " + holdStatus(cargoHold));
2635

2736
/* Steal some fuel from the shuttle:
2837
*/
29-
38+
3039
//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.
3140

3241
//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.
@@ -38,6 +47,12 @@ console.log("Hold status: " + holdStatus(cargoHold));
3847
/* Next, liberate some of that glorious cargo.
3948
*/
4049

50+
let cargoInspector = function (arr) {
51+
let newItem = arr.splice(3, 2, "band-aids", "clippers");
52+
cargoHold = arr;
53+
return newItem;
54+
};
55+
4156
//a). Define another anonymous function with an array as a parameter, and set it equal to another innocent variable.
4257

4358
//b). You need to swipe two items from the cargo hold. Choose well. Stealing water ain’t gonna get us rich. Put the swag into a new array and return it from the function.
@@ -48,9 +63,16 @@ console.log("Hold status: " + holdStatus(cargoHold));
4863

4964
/* Finally, you need to print a receipt for the accountant. Don’t laugh! That genius knows MATH and saves us more gold than you can imagine.
5065
*/
51-
66+
5267
//a). Define a function called irs that can take fuelLevel and cargoHold as arguments.
53-
68+
5469
//b). Call your anonymous fuel and cargo functions from within irs.
5570

56-
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
71+
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
72+
73+
let irs = function (fuel, cargo) {
74+
let arr = cargoInspector(cargo);
75+
return `Raided ${fuelChecker(fuel)} kg of fuel from the tanks, and stole ${arr[0]} and ${arr[1]} from the cargo hold.`;
76+
};
77+
78+
console.log(irs(fuelLevel, cargoHold));

0 commit comments

Comments
 (0)