HOFsAndRecursionMiniChallenges

Use this repo to practice Higher Order Functions (map, filter & reduce) and recursion. The goal is to make all the tests in the spec runner pass!

Pending for this repo

  • optimize the HOFs tests
  • not enough repition to use beforeEach and afterEach logic for some of the tests (if setup not being used in more than one of the it blocks, then not optimal to have hook running on every test. Refactor to run those operations in the one block needing it)

Higer Order Functions (HOFs)

What are HOFs? HOFs are functions that operate on other functions by either taking them in as arguments are returning the. Map, filter and reduce are built-in array methods in javascript

  • Map creates a new array with the results of calling the provided function on every element in the array

  • Filter creates a new array with all elements that pass teh test implemented by the provided function

  • Reduce executes a reducer functino (that you provide) on each element of the array, resulting in a single output value

Recursion

What is recursion? A function that calls itself

When would you use recursion? DOM traversal, tree traversal, graph traversal, when you don't know how many layers / how deeply nested an object or array has.

What are the components of recursion?

  1. Base Case - where the recursion stops
  2. Recursive call to function itself

Recursion and the call stack

When a function is invoked, it is placed on top of the call stack. Functions are removed one at a time from the top of the stack when we hit a return statement. With recursion, the same function gets pushed onto the call stack over and over and over.

How do we get recursion to stop? We invoke the same function with a different input until we reach the base case!

Helper Function Recursion

Helper function recursion works because of closures. An inner helper function is used inside of the main function so the inner funciton has access to the result variable (without the risk of resetting the variable with each iteration).

Helper function recursion requirements

  1. Result variable in the main function
  2. Helper function which contains: Base case and code that adds to the result variable
  3. A recursive call to the helper function inside of the main function
  4. A return statement for the result in the main function

Installation

To get started:

Navigate to the project Directory

$ cd officeHourMiniChallenges

To see the tests:

$ open the project in VS Code, then right click the SpecRunner.html file and select the "Open with Live Server Option" OR drag the SpecRunner.html file into a browser then press ENTER

Helpful Articles

Technologies

Mocha Chai Sinon