From 9d0cd3cc16659df567d2548091aee58ac357939a Mon Sep 17 00:00:00 2001 From: Gage Date: Thu, 14 Sep 2023 16:29:45 -0500 Subject: [PATCH 1/4] 45% completed --- src/underbar.js | 116 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 29 deletions(-) diff --git a/src/underbar.js b/src/underbar.js index 931b20a..e6242ec 100644 --- a/src/underbar.js +++ b/src/underbar.js @@ -1,4 +1,4 @@ -(function() { +(function () { 'use strict'; window._ = {}; @@ -6,7 +6,8 @@ // Returns whatever value is passed as the argument. This function doesn't // seem very useful, but remember it--if a function needs to provide an // iterator when the user does not pass one in, this will be handy. - _.identity = function(val) { + _.identity = function (val) { + return val; }; /** @@ -30,13 +31,14 @@ // Return an array of the first n elements of an array. If n is undefined, // return just the first element. - _.first = function(array, n) { + _.first = function (array, n) { return n === undefined ? array[0] : array.slice(0, n); }; // Like first, but for the last elements. If n is undefined, return just the // last element. - _.last = function(array, n) { + _.last = function (array, n) { + return n === undefined ? array[array.length - 1] : array.slice(-n, (n === 0 ? 0 : array.length)); //migth change in the future }; // Call iterator(value, key, collection) for each element of collection. @@ -44,12 +46,27 @@ // // Note: _.each does not have a return value, but rather simply runs the // iterator function over each item in the input collection. - _.each = function(collection, iterator) { + _.each = function (collection, iterator) { + + if (Array.isArray(collection)) { + + for (let i = 0; i < collection.length; i++) { + iterator(collection[i], i, collection) + } + + } else { + + for (let key in collection) { + iterator(collection[key], key, collection) + } + + } + }; // Returns the index at which value can be found in the array, or -1 if value // is not present in the array. - _.indexOf = function(array, target){ + _.indexOf = function (array, target) { // TIP: Here's an example of a function that needs to iterate, which we've // implemented for you. Instead of using a standard `for` loop, though, // it uses the iteration helper `each`, which you will need to write. @@ -65,22 +82,63 @@ }; // Return all elements of an array that pass a truth test. - _.filter = function(collection, test) { + _.filter = function (collection, test) { + let value = []; + + for (let i = 0; i < collection.length; i++) { + if (test(collection[i], i, collection)) { + value.push(collection[i]); + } + } + return value; }; // Return all elements of an array that don't pass a truth test. - _.reject = function(collection, test) { + _.reject = function (collection, test) { // TIP: see if you can re-use _.filter() here, without simply // copying code in and modifying it + let True = _.filter(collection, test); + let arr = [] + + for (let i = 0; i < collection.length; i++) { + if (!True.includes(collection[i])) { + arr.push(collection[i]) + } + } + return arr; }; // Produce a duplicate-free version of the array. - _.uniq = function(array, isSorted, iterator) { + _.uniq = function (array, isSorted, iterator) { + //create a empty array + let arr = []; + let arr2 = []; + //check if isSorted is given or false + if (!isSorted) { + //loop through the given array + for (let i = 0; i < array.length; i++) { + //check if arr doesn't has a value equal to the current value + if (!arr.includes(array[i])) { + //push the current value into arr + arr.push(array[i]); + } + } + //run if isSorted is true + } else { + //loop through the given array + for (let i = 0; i < array.length; i++) { + if (!arr.includes(array[i]) && iterator(array[i], i, array)) { + arr.push(array[i]); + } + } + } + //return arr + return arr }; // Return the results of applying an iterator to each element. - _.map = function(collection, iterator) { + _.map = function (collection, iterator) { // map() is a useful primitive iteration function that works a lot // like each(), but in addition to running the operation on all // the members, it also maintains an array of results. @@ -95,7 +153,7 @@ // Takes an array of objects and returns and array of the values of // a certain property in it. E.g. take an array of people and return // an array of just their ages - _.pluck = function(collection, key) { + _.pluck = function (collection, key) { // TIP: map is really handy when you want to transform an array of // values into a new array of values. _.pluck() is solved for you // as an example of this. @@ -124,11 +182,11 @@ // return total + number * number; // }); // should be 5, regardless of the iterator function passed in // No accumulator is given so the first element is used. - _.reduce = function(collection, iterator, accumulator) { + _.reduce = function (collection, iterator, accumulator) { }; // Determine if the array or object contains a given value (using `===`). - _.contains = function(collection, target) { + _.contains = function (collection, target) { // TIP: Many iteration problems can be most easily expressed in // terms of reduce(). Here's a freebie to demonstrate! return _.reduce(collection, (wasFound, item) => { @@ -141,13 +199,13 @@ // Determine whether all of the elements match a truth test. - _.every = function(collection, iterator) { + _.every = function (collection, iterator) { // TIP: Try re-using reduce() here. }; // Determine whether any of the elements pass a truth test. If no iterator is // provided, provide a default one - _.some = function(collection, iterator) { + _.some = function (collection, iterator) { // TIP: There's a very clever way to re-use every() here. }; @@ -170,12 +228,12 @@ // }, { // bla: "even more stuff" // }); // obj1 now contains key1, key2, key3 and bla - _.extend = function(obj) { + _.extend = function (obj) { }; // Like extend, but doesn't ever overwrite a key that already // exists in obj - _.defaults = function(obj) { + _.defaults = function (obj) { }; @@ -189,7 +247,7 @@ // Return a function that can be called at most one time. Subsequent calls // should return the previously returned value. - _.once = function(func) { + _.once = function (func) { ////don't do // TIP: These variables are stored in a "closure scope" (worth researching), // so that they'll remain available to the newly-generated function every // time it's called. @@ -198,7 +256,7 @@ // TIP: We'll return a new function that delegates to the old one, but only // if it hasn't been called before. - return function() { + return function () { if (!alreadyCalled) { // TIP: .apply(this, arguments) is the standard way to pass on all of the // information from one function call to another. @@ -218,7 +276,7 @@ // _.memoize should return a function that, when called, will check if it has // already computed the result for the given argument and return that value // instead if possible. - _.memoize = function(func) { + _.memoize = function (func) { /// don't do }; // Delays a function for the given number of milliseconds, and then calls @@ -227,7 +285,7 @@ // The arguments for the original function are passed after the wait // parameter. For example _.delay(someFunction, 500, 'a', 'b') will // call someFunction('a', 'b') after 500ms - _.delay = function(func, wait) { + _.delay = function (func, wait) { /// don't do }; @@ -241,7 +299,7 @@ // TIP: This function's test suite will ask that you not modify the original // input array. For a tip on how to make a copy of an array, see: // http://mdn.io/Array.prototype.slice - _.shuffle = function(array) { + _.shuffle = function (array) { }; @@ -253,14 +311,14 @@ // Calls the method named by functionOrKey on each value in the list. // Note: You will need to learn a bit about .apply to complete this. - _.invoke = function(collection, functionOrKey, args) { + _.invoke = function (collection, functionOrKey, args) { }; // Sort the object's values by a criterion produced by an iterator. // If iterator is a string, sort objects by that property with the name // of that string. For example, _.sortBy(people, 'name') should sort // an array of people by their name. - _.sortBy = function(collection, iterator) { + _.sortBy = function (collection, iterator) { }; // Zip together two or more arrays with elements of the same index @@ -268,24 +326,24 @@ // // Example: // _.zip(['a','b','c','d'], [1,2,3]) returns [['a',1], ['b',2], ['c',3], ['d',undefined]] - _.zip = function() { + _.zip = function () { }; // Takes a multidimensional array and converts it to a one-dimensional array. // The new array should contain all elements of the multidimensional array. // // Hint: Use Array.isArray to check if something is an array - _.flatten = function(nestedArray, result) { + _.flatten = function (nestedArray, result) { }; // Takes an arbitrary number of arrays and produces an array that contains // every item shared between all the passed-in arrays. - _.intersection = function() { + _.intersection = function () { }; // Take the difference between one array and a number of other arrays. // Only the elements present in just the first array will remain. - _.difference = function(array) { + _.difference = function (array) { }; // Returns a function, that, when invoked, will only be triggered at most once @@ -293,6 +351,6 @@ // on this function. // // Note: This is difficult! It may take a while to implement. - _.throttle = function(func, wait) { + _.throttle = function (func, wait) { }; }()); From f66a26a830363efa8ad555139282a5811890e117 Mon Sep 17 00:00:00 2001 From: Gage Date: Fri, 15 Sep 2023 16:08:53 -0500 Subject: [PATCH 2/4] completed 4 more problem and added sudoCode --- src/underbar.js | 86 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/src/underbar.js b/src/underbar.js index e6242ec..263f71d 100644 --- a/src/underbar.js +++ b/src/underbar.js @@ -38,6 +38,9 @@ // Like first, but for the last elements. If n is undefined, return just the // last element. _.last = function (array, n) { + //return the last number if n is not defined + //if n defined return the the the value starting at negative n and ending at the last value in the array + //if n equal to 0 do .slice(-n, 0) return n === undefined ? array[array.length - 1] : array.slice(-n, (n === 0 ? 0 : array.length)); //migth change in the future }; @@ -47,16 +50,18 @@ // Note: _.each does not have a return value, but rather simply runs the // iterator function over each item in the input collection. _.each = function (collection, iterator) { - + //check if the collection is an array if (Array.isArray(collection)) { - + //loop through the given collection for (let i = 0; i < collection.length; i++) { + //run the iterator and give it the current value, it's index, and the array itself iterator(collection[i], i, collection) } - + //run if collection is a object } else { - + //loop through the given collection for (let key in collection) { + //run the iterator and give it the current value, it's key name, and the object itself iterator(collection[key], key, collection) } @@ -83,13 +88,17 @@ // Return all elements of an array that pass a truth test. _.filter = function (collection, test) { + //create an empty array let value = []; - + //loop through the collection for (let i = 0; i < collection.length; i++) { + //check if the given test return true after given it the current value, index and the collection itself if (test(collection[i], i, collection)) { + //push the current value into the value array value.push(collection[i]); } } + //return value return value; }; @@ -97,15 +106,20 @@ _.reject = function (collection, test) { // TIP: see if you can re-use _.filter() here, without simply // copying code in and modifying it - let True = _.filter(collection, test); - let arr = [] + //create two array where one will hold values that past the test and the other array will be empty + let True = _.filter(collection, test); + let False = [] + //loop through the collection for (let i = 0; i < collection.length; i++) { + //check if the current value is not in the True array if (!True.includes(collection[i])) { - arr.push(collection[i]) + //push the current value into the false array + False.push(collection[i]) } } - return arr; + //return the false array + return False; }; // Produce a duplicate-free version of the array. @@ -142,6 +156,15 @@ // map() is a useful primitive iteration function that works a lot // like each(), but in addition to running the operation on all // the members, it also maintains an array of results. + //create a empty array + var arr = []; + //loop through the collection + for (let i = 0; i < collection.length; i++) { + //push the the value return from iterator that was given the current value, it index, and the collection it self + arr.push(iterator(collection[i], i, collection)) + } + //return arr + return arr; }; /* @@ -183,19 +206,60 @@ // }); // should be 5, regardless of the iterator function passed in // No accumulator is given so the first element is used. _.reduce = function (collection, iterator, accumulator) { + //check if accumulator is defined + if (accumulator === undefined) { + //set the accumulator to equal the first value in the collection + accumulator = collection[0]; + //loop through the collection + for (let i = 1; i < collection.length; i++) { + //set the accumulator equal to the return value of the iterator after give it the accumulator, the current value, the index, and the collection + accumulator = iterator(accumulator, collection[i], i, collection) + } + //return the accumulator + return accumulator + } + //loop through the collection + for (let i = 0; i < collection.length; i++) { + //set the accumulator equal to the return value of the iterator after give it the accumulator, the current value, the index, and the collection + accumulator = iterator(accumulator, collection[i], i, collection) + } + //return the accumulator + return accumulator }; // Determine if the array or object contains a given value (using `===`). _.contains = function (collection, target) { // TIP: Many iteration problems can be most easily expressed in // terms of reduce(). Here's a freebie to demonstrate! - return _.reduce(collection, (wasFound, item) => { + //create a empty variable + let array; + //check if collection is an array or object + if (Array.isArray(collection)) { + //set array to equal the collection + array = collection; + //run if collection is a object + } else { + //set array to equal a empty array + array = []; + //loop through the collection + for (let key in collection) { + //push the value from the current key into array + array.push(collection[key]); + } + } + //return the value from reduce that was given the array, a function, and a false value + return _.reduce(array, (wasFound, item) => { + //check if wasFound equal true if (wasFound) { + //return true return true; } + //return true or false if the current item is equal to the given target return item === target; + }, false); - }; + } + // Determine whether all of the elements match a truth test. From 75cb28ded8247400aa870eb4410550cc123e418b Mon Sep 17 00:00:00 2001 From: SpecTrail Date: Mon, 18 Sep 2023 15:02:52 -0500 Subject: [PATCH 3/4] completed 7/8 test for every --- src/underbar.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/underbar.js b/src/underbar.js index 263f71d..b641520 100644 --- a/src/underbar.js +++ b/src/underbar.js @@ -265,6 +265,20 @@ // Determine whether all of the elements match a truth test. _.every = function (collection, iterator) { // TIP: Try re-using reduce() here. + let isTrue = true + if (Array.isArray(collection)) { + let test = _.reduce(collection, function (value, cur) { + // if (cur === false || cur === undefined) { + // return false + // } + if (!iterator(cur) || value === false) { + return false + } + return true + }, true) + + return test + } }; // Determine whether any of the elements pass a truth test. If no iterator is From 10d0644e2ba853d5e9eac6b6a4d4d901d25519c7 Mon Sep 17 00:00:00 2001 From: SpecTrail Date: Mon, 18 Sep 2023 16:27:10 -0500 Subject: [PATCH 4/4] completed every, some, and extend --- src/underbar.js | 69 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/src/underbar.js b/src/underbar.js index b641520..1705c38 100644 --- a/src/underbar.js +++ b/src/underbar.js @@ -265,12 +265,17 @@ // Determine whether all of the elements match a truth test. _.every = function (collection, iterator) { // TIP: Try re-using reduce() here. - let isTrue = true + if (Array.isArray(collection)) { let test = _.reduce(collection, function (value, cur) { - // if (cur === false || cur === undefined) { - // return false - // } + if (iterator === undefined) { + if (cur === false || value === false) { + return false + } else { + return true + } + } + if (!iterator(cur) || value === false) { return false } @@ -285,6 +290,38 @@ // provided, provide a default one _.some = function (collection, iterator) { // TIP: There's a very clever way to re-use every() here. + if (collection === undefined || !Array.isArray(collection) || collection.length === 0) { + return false + } + if (iterator === undefined) { + let noIterator = _.reduce(collection, function (seed, value) { + if (value === true || seed === true) { + return true + } else { + return false + } + }, false) + return noIterator + } + + let test = _.every(collection, function (value) { + if (!iterator(value)) { + return true + } else { + return false + } + }) + + let test2 = _.every(collection, iterator) + if (test2 === true) { + return true + } else if (test === true) { + return false + } else if (test === false) { + return true + } + + }; @@ -306,12 +343,32 @@ // }, { // bla: "even more stuff" // }); // obj1 now contains key1, key2, key3 and bla - _.extend = function (obj) { + _.extend = function (obj, ...obj2) { + + for (let i = 0; i < obj2.length; i++) { + for (let key in obj2[i]) { + + obj[key] = obj2[i][key] + + } + } + + return obj }; // Like extend, but doesn't ever overwrite a key that already // exists in obj - _.defaults = function (obj) { + _.defaults = function (obj, ...obj2) { + + for (let i = 0; i < obj2.length; i++) { + for (let key in obj2[i]) { + if (!obj[key] || obj[key] === NaN) { + obj[key] = obj2[i][key] + } + } + } + + return obj };