11.10. Exercises: More on Functions

Arrr! Welcome back, pirates.

11.10.1. Practice Your Skills

First, create an anonymous function and practice how to use the map method.

  1. Create an anonymous function and set it equal to a variable. Your function should:

    1. If passed a number, return the tripled value.

      Check your solution.

    2. If passed a string, return the string "ARRR!"

    3. If NOT passed a number or string, return the data unchanged.

      Check your solution.

    4. Build your function here, and be sure to test it.

  2. Add to your code! Use your function and the map method to change the array ['Elocution', 21, 'Clean teeth', 100] as follows:

    1. Triple all the numbers.
    2. Replace the strings with "ARRR!"
    3. Print the new array to confirm your work.

11.10.2. Raid a Shuttle

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));

Hack the code at repl.it.

  1. First, steal some fuel from the shuttle:

    1. 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.

      Check your solution.

    2. 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.

    3. Once you figure out how much fuel to pump out, return that value.

      Check your solution.

    4. 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.

  2. Next, liberate some of that glorious cargo.

    1. Define another anonymous function with an array as a parameter, and set it equal to another innocent variable.
    2. 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.
    3. The cargo hold has better security than the fuel tanks. It counts how many things are in storage. You need to replace what you steal with something worthless. The count MUST stay the same, or you'll get caught and thrown into the LaunchCode brig.
    4. Don't get hasty, matey! Remember to test your function.
  3. 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.

    1. Define a function called irs that can take fuelLevel and cargoHold as arguments.

      Check your solution.

    2. Call your anonymous fuel and cargo functions from within irs.

    3. Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."

      Check your solution.