Exercise Solutions: More on FunctionsΒΆ

  1. Create an anonymous function.

    1. 1
      2
      3
      4
      5
      let practice = function(myArg) {
         if (typeof myArg === "number") {
            return myArg * 3;
         }
      }
      
    1.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      let practice = function(myArg) {
         if (typeof myArg === "number") {
            return myArg * 3;
         } else if (typeof myArg === "string") {
            return "ARRR!";
         } else {
            return myArg;
         }
      
      }
      

    Back to the exercises

  1. Create another anonymous function.

    1. 1
      2
      3
      let nonSuspiciousFunction = function(a) {
      
      }
      
    1.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      let nonSuspiciousFunction = function(a) {
         if (checkFuel(a) === 'green') {
            return a - 100001;
         }
         else if (checkFuel(a) === 'yellow') {
            return a - 50001;
         }
         else {
            return a;
         }
      };
      

    Back to the exercises

  1. Print a receipt.

    1. 1
      2
      3
      let irs = function(levelOfFuel, itemsInCargo) {
      
      }
      
    1. 1
      2
      3
      4
      let irs = function(levelOfFuel, itemsInCargo {
         let arr = deckMops(itemsInCargo);
         return `Raided ${nonSuspiciousFunction(fuelLevel)} kg of fuel from the tanks, and stole ${arr[0]} and ${arr[1]} from the cargo hold.`
      }
      

    Back to the exercises