diff --git a/exercises.js b/exercises.js index 4aad72f..d9df927 100644 --- a/exercises.js +++ b/exercises.js @@ -6,27 +6,45 @@ 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 multi = 1; + if (arguments.length === 0) return 0; + for (var i = 0; i < arguments.length; i++) { + multi *= arguments[i]; + } + return multi; } 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 sum = 0; + for (var i = 0; i < numbers.length; i++) { + sum += numbers[i]; + } + cb(sum); } 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 (value) {cb(value);}); } 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 array = []; + for (var i = 0; i < arr.length; i++) { + array.push(cb(arr[i])); + } + return array; } function getUserConstructor() { @@ -36,11 +54,23 @@ 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() { @@ -48,6 +78,9 @@ function addReverseString() { //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) { @@ -55,6 +88,10 @@ function nFactorial(n) { //solve this recursively //example: //the factorial of 3 is 6 (3 * 2 * 1) + if (n === 0) { + return 1; + } + return n * nFactorial(n-1); } function cacheFunction(cb) { @@ -69,6 +106,17 @@ 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 cashe = []; + return ( + function(arg) { + if (cashe.hasOwnProperty(arg)) { + return cashe[arg]; + }else { + cashe[arg] = cb(arg); + return cashe[arg]; + } + } + ); }