1. How do we even do it?
  2. What was your result?

Functions

  • Anonymous vs. Named
    • Anonymous Functions
    • Unnamed, and thus cannot refer to itself (for purposes of recursion or debugging)
    • Named Functions
    • Have a name and thus can refer to themselves, along with several other goodies we'll talk about in a minute...
  • General Requirements:
    • The function keyword followed by...
    • The name of the function (if named)
    • A list of arguments to the functions enclosed in parentheses and separated by commas
    • The statements that define the functions enclosed in curly brackets
  • Function declaration vs. function expression:
    
        //FUNCTION EXPRESSION:
        var logStuff = function(param) {
           console.log(`${param} was passed in`);
        };
    
        //FUNCTION DECLARATION
        function multiply(num, secondNum) {
            return num * secondNum;
        };
        //****IMPORTANT: Function hoisting only works with function declaration and not with function expression.
    
                        
  • Scoping/Closure: variables declared inside can't be seen outside, but inside can access all variables declared in the same/parent scopes.
  • Recursive Functions: Function that calls itself, performs just like a loop.
  • Arrow Functions:
    • New to ES6
    • Cannot be used as contructors (more on this later)
    • No access to lexical this
    • 
              //BASIC SYNTAX
      
              (arg1, arg2) => {
                  // function body here
              }
      
              //OR//
      
              let multiply = (arg1, arg2) => return arg1 * arg2;
      
              //IF THERE'S ONLY 1 ARGUMENT:
      
              arg1 => {alert(arg1)}
      
              //OR//
      
              arg1 => alert(arg1);
      
              //IF THERE ARE NO ARGUMENTS:
      
              () => {
                  console.log('Hello there!');
              }
          
  • .substr()

On your day5 branch, write a function that calculates a dog's age. It should:

  • Accept 1 argument (age of the dog in years).
  • Return the age of the dog using the conversion rate of 1 year to 7 "dog" years.
  • Alert the answer.

Make sure you're checking for edge-cases!

Now, write a function that tells you what a "lifetime supply" will be for any product. It should:

  • Accept 2 arguments (age of person now, amount of product per day).
  • Calculate the supply needed for the rest of the user's life, based on a constant max age of 80.
  • Alert the answer in this format: "You will need x to last you until you're y.
  • BONUS: write it to accept a dynamic max age.

There is a basic, but important difference

  • A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.
  • A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:
    1. A method is implicitly passed the object on which it was called.
    2. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).

We will cover this more in-depth in a future class, but for now, remember the difference.

On your takehome-day5 branch write a program that uses functions to:

  1. Take a number and return the square of that number (power of 2).
  2. If a non-number argument is passed into the function, alert NaN and prompt for another response
  3. In a second function, capitalize the first letter of a string and add a period (.) to the end of the string if it doesn't already end with a period
  4. Create a string that will flip the position of the first and second half of a string. For example, the string “abcdef” would be returned as “defabc”. Hint: use substring.
  5. BONUS: If you are bored by all that, write a function that checks if a given string is a palindrome.