Arrr! Welcome back, pirates.
First, create an anonymous function and practice how to use the map method.
Create an anonymous function and set it equal to a variable. Your function should:
If passed a number, return the tripled value.
If passed a string, return the string "ARRR!"
If NOT passed a number or string, return the data unchanged.
Build your function here, and be sure to test it.
Add to your code! Use your function and the map method to
change the array ['Elocution', 21, 'Clean teeth', 100]
as follows:
You may still be wondering Why would I ever use an anonymous function? For today's mission, of course!
You need to hack into the shuttle code and steal supplies. Since the
LaunchCode TAs keep a sharp eye on the shuttle goodies, you can't just add new functions
like siphonFuel
or lootCargo
. You need to be more sneaky.
Clever.
Invisible.
Anonymous.
The first mate swiped a copy of the code you need to hack:
1function checkFuel(level) {
2 if (level > 100000){
3 return 'green';
4 } else if (level > 50000){
5 return 'yellow';
6 } else {
7 return 'red';
8 }
9}
10
11function 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 }
19}
20
21let fuelLevel = 200000;
22let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold', 'water', 'AE-35 unit'];
23
24console.log("Fuel level: "+ checkFuel(fuelLevel));
25console.log("Hold status: "+ holdStatus(cargoHold));
First, steal some fuel from the shuttle:
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.
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.
Once you figure out how much fuel to pump out, return that value.
Decide where to best place your function call to gather our new fuel.
Tip
Be sure to test your function! Those bilge rat TAs will notice if they lose too much fuel.
Next, liberate some of that glorious cargo.
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.
Define a function called irs
that can take fuelLevel
and
cargoHold
as arguments.
Call your anonymous fuel and cargo functions from within irs
.
Use a template literal to return, "Raided _____ kg of fuel from the
tanks, and stole ____ and ____ from the cargo hold."