diff --git a/assignments/arrays.js b/assignments/arrays.js index 13890658..d1184d7d 100644 --- a/assignments/arrays.js +++ b/assignments/arrays.js @@ -1,4 +1,9 @@ +'use strict'; const items = [1, 2, 3, 4, 5, 5]; // use this array to test your code. + +let cb = () => ""; +cb = ( x ) => x; + /* Complete the following functions. These functions only need to work with arrays. @@ -9,46 +14,112 @@ const items = [1, 2, 3, 4, 5, 5]; // use this array to test your code. You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating */ + +cb = ( x ) => console.log(x-1); function each(elements, cb) { // Do NOT use forEach to complete this function. // Iterates over a list of elements, yielding each in turn to the `cb` function. // This only needs to work with arrays. // You should also pass the index into `cb` as the second argument // based off http://underscorejs.org/#each + for ( let value of elements ) { + cb( value ); + } } +each(items, cb); + +cb = ( x ) => x*2; function map(elements, cb) { // Do NOT use .map, to complete this function. // How map works: Map calls a provided callback function once for each element in an array, in order, and functionructs a new array from the res . // Produces a new array of values by mapping each value in list through a transformation function (iteratee). // Return the new array. + let returnArray = []; + for ( let value of elements ){ + returnArray.push(cb(value)); + } + return returnArray; } +// console.log(map(items, cb)); + +cb = ( reduced, value ) => reduced += value; function reduce(elements, cb, startingValue) { // Do NOT use .reduce to complete this function. // How reduce works: A reduce function combines all elements into a single value going from left to right. // Elements will be passed one by one into `cb` along with the `startingValue`. // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. + let index = startingValue || 0; + const control = elements[index]; + let reduced = control; + for ( let i = index+1; i < elements.length; ++i ) { + reduced = cb(reduced, elements[i] ) + } + return reduced; } +// console.log(reduce(items, cb)); + +cb = ( x ) => x === "valueToCompare" ? true : false; function find(elements, cb) { // Do NOT use .includes, to complete this function. // Look through each value in `elements` and pass each element to `cb`. // If `cb` returns `true` then return that element. // Return `undefined` if no elements pass the truth test. + let found = undefined; + for ( let i = 0; i < elements.length; ++i) { + if ( cb(elements[i]) ) { return found = elements[i] } + } + return found; } +let something = [undefined, null,"a"]; +// console.log(find(items, cb)); +// console.log(find(something, cb)); + +cb = ( x ) => x === "anyFilter" ? true : false; function filter(elements, cb) { // Do NOT use .filter, to complete this function. // Similar to `find` but you will return an array of all elements that passed the truth test // Return an empty array if no elements pass the truth test + let filtered =[]; + for ( let value of elements ){ + cb(value) ? filtered.push(value) : null; + } + return filtered; } + const nestedArray = [1, [2], [[3]], [[[4]]]]; // use this to test 'flatten' +const nestedArray2 = [[[[4]]],5, [2], [[3]], [[[4]]]]; // use this to test 'flatten' +const nestedArray3 = [[[[]]],,[], [[]], [[[]]]]; // use this to test 'flatten' function flatten(elements) { // Flattens a nested array (the nesting can be to any depth). // Hint: You can solve this using recursion. // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + let toFlat = []; + + function removeArray ( item ) { + Array.isArray( item ) ? checkItems(item) : toFlat.push(item); //? + } + + function checkItems ( array ) { + for ( let item of array ) { + console.log(item); + removeArray( item ); + } + } + + checkItems(elements); + return toFlat; + + } + +console.log(flatten(nestedArray)); +console.log(flatten(nestedArray2)); +console.log(flatten(nestedArray3)); + diff --git a/assignments/closure.js b/assignments/closure.js index ec4c4cab..15027ecb 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,15 +1,47 @@ +'use strict' + function counterFactory() { // Return an object that has two methods called `increment` and `decrement`. // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. + let counter = 0; + function increment () { + return ++counter;; + } + function decrement () { + return --counter; + } + return { + increment, + decrement + } } +console.log(counterFactory().increment()); +console.log(counterFactory().decrement()); + + +function cb () { console.log("Callback invoked") }; function limitFunctionCallCount(cb, n) { // Should return a function that invokes `cb`. // The returned function should only allow `cb` to be invoked `n` times. // Returning null is acceptable if cb can't be returned + let counter = n; + function invoke () { + // counter-- > 0 ? cb() : throw(new Error (`Callback already called ${n}-times`)); I CAN NOT FIGURE OUT WHY THIS LINE IS NO WORKING + counter-- > 0 ? cb() : console.log(`Callback already called ${n}-times`); + } + return {invoke}; } +let nTimes = limitFunctionCallCount(cb, 3); +nTimes.invoke(); +nTimes.invoke(); +nTimes.invoke(); +nTimes.invoke(); + +cb = x => `Good! argument ${x} was no used!` +// console.log(cb(1)); function cacheFunction(cb) { // Should return a funciton that invokes `cb`. // A cache (object) should be kept in closure scope. @@ -17,4 +49,21 @@ function cacheFunction(cb) { // If the returned function is invoked with arguments that it has already seen // then it should return the cached result and not invoke `cb` again. // `cb` should only ever be invoked once for a given set of arguments. + const cache = new Set(); + + function invoke ( param ) { + if (!cache.has(param)) { + console.log(cache); + cache.add(param); + return cb(param); + } + else { + console.log(cache); + return cache; + } + } + return {invoke}; } +let argumentUsed = cacheFunction(cb); +console.log(argumentUsed.invoke("x")); +console.log(argumentUsed.invoke("x")); \ No newline at end of file diff --git a/assignments/objects.js b/assignments/objects.js index 07930ded..82377b32 100644 --- a/assignments/objects.js +++ b/assignments/objects.js @@ -1,5 +1,5 @@ const testObject = { name: 'Bruce Wayne', age: 36, location: 'Gotham' }; // use this object to test your functions - +let cb = x => x; // Complete the following underscore functions. // Reference http://underscorejs.org/ for examples. @@ -7,23 +7,48 @@ function keys(obj) { // Retrieve all the names of the object's properties. // Return the keys as strings in an array. // Based on http://underscorejs.org/#keys + return Object.keys(obj); } +console.log(keys(testObject)); + function values(obj) { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + const values = []; + for ( let prop in obj ) { + (typeof obj[prop] === "function") ? null : values.push(obj[prop]) + } + return values; } +console.log(values(testObject)); + +cb = x => "maped: "+x; function mapObject(obj, cb) { // Like map for arrays, but for objects. Transform the value of each property in turn by passing it to the callback function. // http://underscorejs.org/#mapObject + const mapped = {} + for ( let prop in obj ){ + mapped[prop] = cb(obj[prop]); + } + return mapped; } +console.log(mapObject(testObject, cb)); + + function pairs(obj) { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + + /* Seems that Object.entries() is not supported in my editor, but is supported in Chrome Developer Tools */ + return Object.entries(obj); } +console.log(pairs(testObject)); + + /* STRETCH PROBLEMS */ @@ -31,7 +56,17 @@ function invert(obj) { // Returns a copy of the object where the keys have become the values and the values the keys. // Assume that all of the object's values will be unique and string serializable. // http://underscorejs.org/#invert + const key = Object.keys(obj); + /* Seems that Object.values() is not supported in my editor, but is supported in Chrome Developer Tools */ + const values = Object.values(obj); + const inverted = [] + for ( let i = 0; i < key.length; i++) { + inverted[i] = [values[i],key[i]]; + }; + return inverted; } +console.log(invert(testObject)); + function defaults(obj, defaultProps) { // Fill in undefined properties that match properties on the `defaultProps` parameter object.