Skip to content

Commit

Permalink
finished homework SunJieMing#4
Browse files Browse the repository at this point in the history
  • Loading branch information
Smithface committed Jan 5, 2018
1 parent dd53b22 commit 8862192
Show file tree
Hide file tree
Showing 2 changed files with 3,702 additions and 0 deletions.
72 changes: 72 additions & 0 deletions exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,56 @@ 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
var total = 1;
if (arguments.length > 0) {
for (var i = 0; i < arguments.length; i++) {
total *= arguments[i];
}
return total;
} else {
return 0;
}
}

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 result = 0;
for (var i = 0; i < numbers.length; i++) {
result += numbers[i];
}
cb(result);
}

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)
for (var i = 0; i < arr.length; i++) {
cb(arr[i]);
}
}

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
var newArr = arr.map(function(num) {
return cb(num);
});
/*for (var i = 0; i < arr.length; i++) {
newArr.push(cb(arr[i]));
}*/
// arr.forEach( function(num) {
// newArr.push(cb(num));
// });

return newArr;
}

function getUserConstructor() {
Expand All @@ -34,25 +63,47 @@ 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
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;
};
}
return User;
}

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
var result = '';
String.prototype.reverse = function() {
for (var i = 0; i < this.length; i++) {
result = this[i] + result;
}
return result;
};
}

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

function cacheFunction(cb) {
Expand All @@ -67,6 +118,27 @@ 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 cache = [];
var results = [];
var limitate = function(arg) {
var argIsThere = false;
var numindex = 0;
cache.forEach(function(num, i) {
if (arg === num) {
argIsThere = true;
numindex = i;
}
});
if (argIsThere) {
return results[numindex];
} else{
var result = cb(arg);
cache.push(arg);
results.push(result);
return result;
}
};
return limitate;
}


Expand Down

0 comments on commit 8862192

Please sign in to comment.