ajpenalosa
12/2/2017 - 6:03 AM

Function

The Function constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and performance issues similar to eval.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

// FUNCTIONS
// ================================================================================

// Creates a Function called "adder" that takes three arguments (x, y, z).
function adder(x, y, z) {

  // The function should add the arguments together, and log the result to the console.
  console.log(x + y + z);
}

// We can also create a version of "adder" that returns its result.
function adderReturn(x, y, z) {
  return x + y + z;
}

// Creates a Function called "multiplier" that takes three arguments (x, y, z).
function multiplier(x, y, z) {

  // The function should multiply the arguments, and log the result to the console.
  console.log(x * y * z);
}

// We can also create a version of "multiplier" that returns its result.
function multiplierReturn(x, y, z) {
  return x * y * z;
}

// Creates a Function called "isString" that takes three arguments (x, y, z).
function isString(x, y, z) {

  // isString checks if each argument is a string using typeof.

  // If each argument is a string it will note that.
  if (typeof x === "string" && typeof y === "string" && typeof z === "string") {
    console.log("Yep. They are all strings");
  }

  // If anyone of them is NOT a string... it will note that.
  else {
    console.log("No... I don't believe these are strings.");
  }
}

// Creates a Function called "vowelChecker" that takes in a single argument (x).
function vowelChecker(x) {

  // vowelChecker will grab the first letter (character)...
  var firstChar = x.toLowerCase().charAt(0);

  // Then check if that first letter is a vowel.
  if (firstChar === "a" || firstChar === "e" || firstChar === "i" || firstChar === "o" || firstChar === "u") {

    // If so... it will log that.
    console.log("OMG. The first letter is a vowel!");

  }

  else {

    // If not... it will log that.
    console.log("First letter is NOT a vowel. *sadface*");

  }
}

// FUNCTION EXECUTION
// ================================================================================

// Each of the below lines of code is what actually "calls" or "runs" the functions.
// Without the below code, the functions above are like workers just waiting to be called into action.

// Calling our adder function.
adder(1, 2, 3);

console.log("-------------------");

// If we use the version that returns its result:
var result = adderReturn(1, 2, 3);
console.log(result);

console.log("-------------------");

// Calling our multiplier function.
multiplier(2, 3, 4);

console.log("-------------------");

// If we use the version that returns its result:
result = multiplierReturn(2, 3, 4);
console.log(result);

console.log("-------------------");

// Calling our isString function.
isString("Ahmed", "Bad", "Monkey");
isString(2, "Way", "Street");

console.log("-------------------");

// Calling our vowelChecker function.
vowelChecker("Eek");
vowelChecker("Nah");