Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Homework #4 #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Feynman Writing Prompts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Callback Functions

Callback functions are functions that you pass as arguments to other functions. You can have a function
call a variety of different functions inside of itself.

Closure

A closure allows functions inside of other functions to use the local variables that are enclosed in the
outer functions scope across calls. You must return the enclosed function from the outer function to achieve
the behavior of a closure.

arguments

The arguments keyword allows you to access the arguments passed into a function array style. It is essentially
an array full of the arguments passed to the function.

Recursion

Recursion is when a function calls itself. All problems solved recursively require identifying the base case.
The base case is the case that the function quits calling itself. A lot of times you use the
return results of previous recursive calls when calculating the return value of a recursive function.

prototype

An objects prototype contains all of its methods. When you instantiate a certain object type the instance
gains all of the methods of that object type's prototype. You can modify prototypes at runtime to change
what methods an instance of an object is created with.

Constructors

Constructors are special types of functions. If we had a constructor called Person, you would use
var person = new Person(); to create an instance of a person. The constructor returns an object with a certain
set of methods and properties that it fills out the same way every time.
54 changes: 54 additions & 0 deletions exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,51 @@ function multiplyArguments() {
// use the arguments keyword to multiply all of the arguments together and return the product
// if no arguments are passed in return 0
// if one argument is passed in just return it
if (arguments.length < 1) {
return 0;
} else if (arguments.length == 1) {
return arguments[0];
}

var result = arguments[0];
for (var i = 1; i < arguments.length; i++) {
result *= arguments[i];
}
return result;
}

function invokeCallback(cb) {
// invoke cb
cb();
}

function sumArray(numbers, cb) {
// sum up all of the integers in the numbers array
// pass the result to cb
// no return is necessary
var total = 0;
for (var i = 0; i < numbers.length; i++) {
total += numbers[i];
}
cb(total);
}

function forEach(arr, cb) {
// iterate over arr and pass its values to cb one by one
// hint: you will be invoking cb multiple times (once for each value in the array)
arr.forEach(function(elem) {
cb(elem);
});
}

function map(arr, cb) {
// create a new array
// iterate over each value in arr, pass it to cb, then place the value returned from cb into the new arr
// the new array should be the same length as the array argument
return arr.map(function(elem) {
elem = cb(elem);
return elem;
});
}

function getUserConstructor() {
Expand All @@ -34,25 +58,46 @@ function getUserConstructor() {
// the constructor should have a method 'sayHi' on its prototype that returns the string 'Hello, my name is {{name}}'
// {{name}} should be the name set on each instance
// return the constructor
return function User(options) {
this.username = options.username;
this.name = options.name;
this.email = options.email;
this.password = options.password;

this.sayHi = function() {
return 'Hello, my name is {{' + this.name + '}}';
};
};
}

function addPrototypeMethod(Constructor) {
// add a method to the constructor's prototype
// the method should be called 'sayHi' and should return the string 'Hello World!'
Constructor.prototype.sayHi = function() {
return 'Hello World!';
};
}

function addReverseString() {
// add a method to the string constructor's prototype that returns a reversed copy of the string
// name this method reverse
// hint:
// you will need to use 'this' inside of reverse
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
}

function nFactorial(n) {
// return the factorial for n
// solve this recursively
// example:
// the factorial of 3 is 6 (3 * 2 * 1)
if (n > 0) {
return n * nFactorial(n-1);
} else {
return 1;
}
}

function cacheFunction(cb) {
Expand All @@ -67,6 +112,15 @@ function cacheFunction(cb) {
// if the function you return is invoked with 5 it would pass 5 to cb(5) and return 25
// if the function you return is invoked again with 5 it will look on an object in the closure scope
// and return 25 directly and will not invoke cb again
var mapobj = {};
return function(elem) {
if (elem in mapobj) {
return mapobj[elem];
} else {
mapobj[elem] = cb(elem);
return mapobj[elem];
}
};
}


Expand Down
Loading