diff --git a/README.md b/README.md index 143243273..555be353e 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ With some basic JavaScript principles in hand, we can now expand our skills out **Follow these steps to set up and work on your project:** -* [ ] Create a forked copy of this project. -* [ ] Add your team lead as collaborator on Github. -* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!). -* [ ] Create a new branch: git checkout -b ``. -* [ ] Implement the project on your newly created `` branch, committing changes regularly. -* [ ] Push commits: git push origin ``. +* [x] Create a forked copy of this project. +* [x] Add your team lead as collaborator on Github. +* [x] Clone your OWN version of the repository (Not Lambda's by mistake!). +* [x] Create a new branch: git checkout -b ``. +* [x] Implement the project on your newly created `` branch, committing changes regularly. +* [x] Push commits: git push origin ``. **Follow these steps for completing your project.** diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..bf589f612 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,17 +57,33 @@ const runners = [ // ==== Challenge 1: Use .forEach() ==== // The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names and populate a new array called `fullNames`. This array will contain just strings. +//let fullNames = []; +//for (let i = 0; i < runners.length; i++) { +// fullNames.push(runners[i].first_name.concat(" " + runners[i].last_name)) +//} +//console.log(fullNames); + let fullNames = []; +for(let i = 0; i < runners.length; i++){ +runners.forEach(function(name){ + fullNames.push(runners[i].first_name.concat(" " + runners[i].last_name)) +}) +} console.log(fullNames); - // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings. let firstNamesAllCaps = []; +runners.map( + (name = name => { + let uppercase = name.first_name.toUpperCase(); + firstNamesAllCaps.push(uppercase); + }) +); console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects. -let runnersLargeSizeShirt = []; +let runnersLargeSizeShirt = runners.filter(shirt => shirt.shirt_size === 'L'); console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..7f984255c 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -37,33 +37,67 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; const test2 = firstItem(items, logExorbitantPrice); console.log(test2); // "this Pencil is worth a million dollars!" */ +//callback function +function findLength(x) { + console.log(x) +} - +//function given callback function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + return cb(arr.length); } -function last(arr, cb) { - // last passes the last item of the array into the callback. -} +getLength(items, findLength) -function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. +function findLast(x) { + console.log(x) } -function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. +function last(arr, cb) { + return cb(arr[3]) } -function contains(item, list, cb) { - // contains checks if an item is present inside of the given array/list. - // Pass true to the callback if it is, otherwise pass false. -} +last(items, findLast) -/* STRETCH PROBLEM */ +//addUp is callback -function removeDuplicates(array, cb) { - // removeDuplicates removes all duplicate values from the given array. - // Pass the duplicate free array to the callback function. - // Do not mutate the original array. -} + //function callback is being passed into + function sumNums(x, y, callback) { + return callback(x + y); + } + + sumNums(3, 4, function(addUp){ + console.log(addUp) + }) + + function multiplyNums(x, y, callback) { + return callback(x * y); + } + + multiplyNums(50, 666, function(multiply){ + console.log(multiply) ; + }) + + function contain (item, list) { + for (let i = 0; i < list.length; i++) { + if (list[i] === item) { + return true; + } else { + return false; + } + } + } + + // contains checks if an item is present inside of the given array/list. + // Pass true to the callback if it is, otherwise pass false. + + + contains(items, ) + + /* STRETCH PROBLEM */ + + function removeDuplicates(array, cb) { + // removeDuplicates removes all duplicate values from the given array. + // Pass the duplicate free array to the callback function. + // Do not mutate the original array. + } diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..500b20afc 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,7 +3,17 @@ // Keep it simple! Remember a closure is just a function // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function ownClosure() { + let name = "Najja O'Connor"; + function alertDisplay() { + console.log("My name is" + ":" + " " + name); + } + return alertDisplay; +} + +let myFunction = ownClosure(); +myFunction(); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ @@ -12,11 +22,19 @@ const counterMaker = () => { // IMPLEMENTATION OF counterMaker: // 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`! + let count = 0; // 2- Declare a function `counter`. It should increment and return `count`. // NOTE: This `counter` function, being nested inside `counterMaker`, // "closes over" the `count` variable. It can "see" it in the parent scope! + function counter() { + return count++; + } // 3- Return the `counter` function. + return counter; }; +counter() = counterMaker(); +myCounter(); +myCounter(); // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2 @@ -27,7 +45,15 @@ const counterMaker = () => { // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== const counterFactory = () => { + increment: function increment(num) { + let counter = 0; + return num++; + } + decrement: function decrement (num) { + return counter--; + } // 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. }; +console.log(counterFactory()); \ No newline at end of file