diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..fbced5738 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "editor.fontFamily": "Fantasque Sans Mono, Hack, Envy Code R VS, Menlo, OperatorMono-Book, SF Pro, Inconsolata, Anonymous Pro, Monaco, 'Courier New', monospace", + "editor.fontLigatures": true, + "editor.fontSize": 15, + "terminal.integrated.fontFamily": "Hack, Envy Code R VS, Menlo, OperatorMono-Book, SF Pro, Inconsolata, Anonymous Pro, Monaco, 'Courier New', monospace", + "terminal.integrated.fontSize": 11 +} \ No newline at end of file diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..a3d05cec9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,28 +58,77 @@ 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 = []; +runners.forEach(function (runnerObj){ + return fullNames.push(`${runnerObj.first_name} ${runnerObj.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 = []; +firstNamesAllCaps = runners.map(function(runnerObj){ + return runnerObj.first_name.toUpperCase(); +}); 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 = []; +runnersLargeSizeShirt = runners.filter(function(runnerObj){ + if (runnerObj.shirt_size === 'L' || runnerObj.shirt_size === 'XL' || runnerObj.shirt_size === '2XL' || runnerObj.shirt_size === '3XL' || runnerObj.shirt_size === '4XL') { + return true; + } +}); + console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== // The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable. let ticketPriceTotal = 0; +//Use Map to extract the donations to a new array +runnerDonations = runners.map(function(runnerObj){ + return runnerObj.donation; +}); +//Use reduce to tally up the new array +ticketPriceTotal = runnerDonations.reduce(function(accumulator, currentValue){ + return (accumulator + currentValue); +}); +//Print out the solution console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== // Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above. -// Problem 1 +// Problem 1: Create Runners Full Names and Donations in a string + +let fullNameAndDonations =[]; +runners.forEach(runnerObj => { + fullNameAndDonations.push(`${runnerObj.first_name} ${runnerObj.last_name} gave $${runnerObj.donation}.`); +}); +//Print out +console.log(fullNameAndDonations); // Problem 2 +//Big Givers: Filter every Runner who gave above $200 so we can send them a personalized Thank You Card. +let bigDonors = runners.filter(runnerObj => runnerObj.donation >= 200); +//Print out +console.log(bigDonors); + +// Problem 3 +//List out all the companies and find out how many runners came from each Company +//String template: ____ people from ____ company participated. +let runnerCompanies = runners + .map(runnerObj => runnerObj.company_name) + //Sorting all Companies alphabetically + .sort(); +//Print out +console.log (runnerCompanies); +//Counter for Each Company +let runnerCompanyNumbers = runnerCompanies.forEach(runnerObj => { +//if a===b, add 1; +//else skip to next and resume counter +}); + + + -// Problem 3 \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..c93cd574f 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,7 +2,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* + /* // GIVEN THIS PROBLEM: @@ -38,27 +38,59 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ - +//My CODE STARTS HERE function getLength(arr, cb) { // getLength passes the length of the array into the callback. -} + cb(arr.length); +}; + + +getLength(items, (lengthoflist) => { + console.log(lengthoflist); +}); + +// TEST NOT WORKING! +// const test4 = showLength (items, getLength) { +// return `${getLength} is how long ${items} array is`; +// } +// console.log(test4); function last(arr, cb) { // last passes the last item of the array into the callback. -} + return cb(arr[arr.length-1]); +}; function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. -} + return cb(x + y); +}; function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. -} + return cb(x * y); +}; 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. -} + + // TRIED USING A FILTER + // function cb = list.filter((itemIterator) + // {return true + // }); + + function cb (item, list) { + for (i = 0; i < list.length; i++) { + if (list[i] === item){ + return true; + } else { + return false; + }// End of If Statement + }// End of For Loop + }; //end of Callback +}; // end of main funtion + + /* STRETCH PROBLEM */ @@ -67,3 +99,5 @@ function removeDuplicates(array, cb) { // Pass the duplicate free array to the callback function. // Do not mutate the original array. } + +//Chech Arrayzing. \ No newline at end of file diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..c0cfe32ff 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,6 +4,13 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. + let dance = function (partner1, partner2, typeOfDance) { + // partner1 = function () { + // return `I am ${partner1}!`; + // }; + return `I am ${partner2}, and I don't like ${typeOfDance}ing with ${partner1}!`; + }; + console.log (dance ('Timothy', 'Elizabeth', 'waltz')); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ @@ -12,14 +19,25 @@ 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`. + let counter = () => { + return count++; + }; // NOTE: This `counter` function, being nested inside `counterMaker`, // "closes over" the `count` variable. It can "see" it in the parent scope! // 3- Return the `counter` function. + return counter; }; -// Example usage: const myCounter = counterMaker(); -// myCounter(); // 1 -// myCounter(); // 2 +//NO ERROR BUT NOT WORKING! +let myCounter = counterMaker(); +myCounter(); +myCounter(); +myCounter(); +// counterMaker(); +// counterMaker(); +//myCounter(); // 1 +//myCounter(); // 2 // ==== Challenge 3: Make `counterMaker` more sophisticated ==== // It should have a `limit` parameter. Any counters we make with `counterMaker` @@ -28,6 +46,18 @@ const counterMaker = () => { // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== const counterFactory = () => { // Return an object that has two methods called `increment` and `decrement`. + return { + increment: function() { + counter++; + return counter; + }, + decrement: function() { + counter --; + return counter; + } + }; // `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