Skip to content

Commit 3dca9c6

Browse files
committed
finished exercises
1 parent 104d01e commit 3dca9c6

1 file changed

Lines changed: 80 additions & 19 deletions

File tree

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,60 @@
11
function checkFuel(level) {
2-
if (level > 100000){
3-
return 'green';
4-
} else if (level > 50000){
5-
return 'yellow';
2+
if (level > 100000) {
3+
return "green";
4+
} else if (level > 50000) {
5+
return "yellow";
66
} else {
7-
return 'red';
7+
return "red";
88
}
99
}
1010

11-
function holdStatus(arr){
11+
function holdStatus(arr) {
1212
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.`;
13+
return `Spaces available: ${7 - arr.length}.`;
14+
} else if (arr.length > 7) {
15+
return `Over capacity by ${arr.length - 7} items.`;
1616
} else {
1717
return "Full";
1818
}
1919
}
2020

2121
let fuelLevel = 200000;
22-
let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold', 'water', 'AE-35 unit'];
22+
let cargoHold = [
23+
"meal kits",
24+
"space suits",
25+
"first-aid kit",
26+
"satellite",
27+
"gold",
28+
"water",
29+
"AE-35 unit",
30+
];
2331

2432
console.log("Fuel level: " + checkFuel(fuelLevel));
2533
console.log("Hold status: " + holdStatus(cargoHold));
2634

27-
/* Steal some fuel from the shuttle:
28-
* /
29-
35+
// Steal some fuel from the shuttle:
36+
3037
//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.
3138

3239
//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.
3340

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

3643
//d). Decide where to best place your function call to gather our new fuel.
44+
let nonSuspiciousFunction = function (a) {
45+
if (checkFuel(a) === "green") {
46+
return a - 100001;
47+
} else if (checkFuel(a) === "yellow") {
48+
return a - 50001;
49+
} else {
50+
return a;
51+
}
52+
};
53+
54+
fuelLevel = nonSuspiciousFunction(fuelLevel);
55+
//placed above at line 23
3756

38-
/* Next, liberate some of that glorious cargo.
39-
* /
57+
// Next, liberate some of that glorious cargo.
4058

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

@@ -46,12 +64,55 @@ console.log("Hold status: " + holdStatus(cargoHold));
4664

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

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-
* /
51-
67+
let stealFromCargoHold = function (cargoHold) {
68+
if (cargoHold.length < 2) {
69+
console.log("Not enough items to steal.");
70+
return cargoHold;
71+
}
72+
let stolenItems = [cargoHold[0], cargoHold[1]];
73+
74+
cargoHold[0] = "stolen 1";
75+
cargoHold[1] = "stolen 2";
76+
77+
return stolenItems;
78+
};
79+
80+
console.log("Cargo hold before theft:", cargoHold);
81+
82+
let stolenItems = stealFromCargoHold(cargoHold);
83+
84+
console.log("Stolen items:", stolenItems);
85+
console.log("Cargo hold after theft:", cargoHold);
86+
87+
// 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.
88+
5289
//a). Define a function called irs that can take fuelLevel and cargoHold as arguments.
53-
90+
5491
//b). Call your anonymous fuel and cargo functions from within irs.
5592

5693
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
5794

95+
let irs = function (fuelLevel, cargoHold) {
96+
let stealFuel = function (level) {
97+
if (checkFuel(level) === "green") {
98+
return level - 100001;
99+
} else if (checkFuel(level) === "yellow") {
100+
return level - 50001;
101+
} else {
102+
return level;
103+
}
104+
};
105+
};
106+
let stolenFuel = stealFuel(fuelLevel);
107+
let stolenCargo = stealFromCargoHold(cargoHold);
108+
109+
let receipt = `Raided ${
110+
fuelLevel - stolenFuel
111+
} kg of fuel from the tanks, and stole ${stolenCargo[0]} and ${
112+
stolenCargo[1]
113+
} from the cargo hold.`;
114+
115+
return receipt;
116+
117+
let raidReceipt = irs(fuelLevel, cargoHold);
118+
console.log(raidReceipt);

0 commit comments

Comments
 (0)