Exercise Solutions: More on FunctionsΒΆ

  1. Create an anonymous function.

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

    Back to the exercises

  1. Create another anonymous function.

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

    Back to the exercises

  1. Print a receipt.

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

    Back to the exercises