Code Monkey home page Code Monkey logo

hofsandrecursionminichallenges's Introduction

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

hofsandrecursionminichallenges's People

Contributors

dlblom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.