From 4bc3e8369dec839ec341dcef39c9ce175086b4eb Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 12:57:30 -0700 Subject: [PATCH 01/10] edits to callbacks --- assignments/callbacks.js | 49 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..0caadbd09 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,19 +38,64 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ - +//ES5 function getLength(arr, cb) { // getLength passes the length of the array into the callback. -} + return cb(arr.length); +} + +getLength(items, function(arrLength) { + console.log(arrLength); +}); + +// ES6 way +const getLength2 = (arr, cb) => { + cb(arr.length); +}; +getLength2(items, length => { + console.log(`This the the length of the array: ${length}.`); +}); + + +//ES5 function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length -1]); +} + +last(items, function(lastItem) { + console.log(lastItem); +}); + +//ES6 +const last2 = (arr, cb) => { + cb(arr[arr.length - 1]); } +last2(items, lastItem => { + console.log(`This is the last item of the array: ${lastItem}.`); +}); + +//ES5 function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); } +sumNums(666, 420, function(x,y) { + console.log(x + y); +}); + +//ES6 +const sumNums2 = (x, y, cb) => { + cb(x + y); +} + +sumNums2(420, 666, sumOfTwo => { + console.log(`This is the sum of any two numbers: ${sumOfTwo}.`); +}); + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. } From d3ba26e50a236fd0a27e8c943004692366be0841 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 13:19:53 -0700 Subject: [PATCH 02/10] finished all problems on callbacks, working on stretch problem --- assignments/callbacks.js | 44 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 0caadbd09..222da8cc9 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -93,18 +93,60 @@ const sumNums2 = (x, y, cb) => { } sumNums2(420, 666, sumOfTwo => { - console.log(`This is the sum of any two numbers: ${sumOfTwo}.`); + console.log(`This is the sum of the chosen two numbers: ${sumOfTwo}.`); }); + +//ES5 function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x, y); +} + +multiplyNums(666, 420, function(x, y) { + console.log(x * y); +}); + +//ES6 +const multiplyNums2 = (x, y, cb) => { + cb(x * y); } +multiplyNums2(420, 666, multipleOfTwo => { + console.log(`This is the multiple of the chosen two numbers: ${multipleOfTwo}.`); +}); + + +//first way to do it 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. + if (list.includes(item)) { + return cb(`Yes, this list contains your item - ${item}.`); + } else { + return cb(`No, this list does not contain your item - ${item}.`); + } } +contains('yo-yo', items, function(check) { + console.log(check); +}) + +//second way to do it +const contains2 = (arr, str, cb) => { + const inArray = () => { + for (let i = 0; i < arr.length; i++) { + if (arr[i] === str) { + return `This list contains ${str}.`; + } + } return `This list does not contain ${str}.`; + }; + cb(inArray()); +}; +contains2(items, 'yo-yo', check => { + console.log(check); +}); + /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { From 0f75b29ad7920b5838204b1bbfe56f3203988686 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 13:27:34 -0700 Subject: [PATCH 03/10] callbacks stretch problem --- assignments/callbacks.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 222da8cc9..81b5f2f69 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -149,8 +149,21 @@ contains2(items, 'yo-yo', check => { /* STRETCH PROBLEM */ -function removeDuplicates(array, cb) { +const arrayWithDupes = [6, 666, 13, 420, 99, 'cat', 'tiger', 'cheetah', 'leopard', 666, 420, 13, 'tiger', 'snow leopard', 'jaguar', 'panther', 'lucifer', 'satan'] + +function removeDuplicates(array, callback) { // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. + return callback(array); } + +console.log(removeDuplicates(arrayWithDupes, function(array) { + let noDupesArray = []; + array.forEach(item => { + if (!noDupesArray.includes(item)) { + noDupesArray.push(item); + } + }); + return noDupesArray; +})); From 7a5780e1538e8689fa1a6017605cef045cf4bebd Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 13:45:43 -0700 Subject: [PATCH 04/10] array methods challenges 1 - 4 done --- assignments/array-methods.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..9dbc5b23e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,23 +58,51 @@ 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(person){ + return fullNames.push(`${person.first_name} ${person.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 = []; +// let firstNamesAllCaps = []; + +const firstNamesAllCaps = runners.map(function(currentValue) { + return currentValue.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 = []; +//let runnersLargeSizeShirt = []; + +const runnersLargeSizeShirt = runners.filter(function(currentValue) { + return currentValue.shirt_size === "L"; +}) + 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; +// let ticketPriceTotal = 0; + +const ticketPriceTotal = runners.reduce(function(accumulator, currentValue) { + // console.log(`I am the accumulator - ${accumulator}.`); + // console.log(`I am the currentValue - ${currentValue}.`); + + return accumulator + currentValue.donation; +}, 0) + 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. From e235fafb825e6d678721cc127f04dee2c57bb675 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 14:02:09 -0700 Subject: [PATCH 05/10] finished array methods --- assignments/array-methods.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 9dbc5b23e..00dc43147 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -81,9 +81,7 @@ console.log(firstNamesAllCaps); // 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 = []; -const runnersLargeSizeShirt = runners.filter(function(currentValue) { - return currentValue.shirt_size === "L"; -}) +const runnersLargeSizeShirt = runners.filter(currentValue => currentValue.shirt_size === "L"); console.log(runnersLargeSizeShirt); @@ -106,8 +104,21 @@ 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 - donations over 250$ +const bigBallers = runners.filter(currentValue => currentValue.donation >= 250); + +console.log(bigBallers); + +// Problem 2 - print out array with full name and contact email +let contactCard = []; + +runners.forEach(person => contactCard.push(`${person.first_name} ${person.last_name} - ${person.email}`)); + +console.log(contactCard); + + +// Problem 3 - company names in all caps -// Problem 2 +const companyAllCaps = runners.map(currentValue => currentValue.company_name.toUpperCase()); -// Problem 3 \ No newline at end of file +console.log(companyAllCaps); \ No newline at end of file From 531367d1750701dc831e58ebb70ed826d439b583 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 14:41:08 -0700 Subject: [PATCH 06/10] played around with nesting and closure example --- assignments/closure.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..5eb3d872a 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -5,6 +5,46 @@ // The outer scope can be a parent function, or the top level of the script. +//NESTED +function genre(genreName, genreName2) { + const origin1 = 'Japan'; + const origin2 = 'Russia'; + console.log(`These are my favorite genres: ${genreName} and ${genreName2}.`); + //debugger; + + function author(authorName, authorName2) { + const lifeStatus = 'dead'; + const lifeStatus2 = 'alive'; + console.log(`${authorName} is ${lifeStatus2}; ${authorName2} is ${lifeStatus}.`); + //debugger; + + function book(title, title2) { + const length = 'light reading'; + const length2 = 'heavy read'; + console.log(`${title} is a ${length2}; ${title2} is some ${length}.`); + //debugger; + } //closes book + book('Wind-Up Bird Chronicle', 'Notes from Underground'); + } //closes author + author('Haruki Murakami', 'Fyodor Dostoevsky'); +} //closes genre +genre('Surrealism', 'Russian Classics'); + + +//Closure +const sub = (function() { + var counter = 0; + return function () { + counter += 1; + return counter; + } +}) (); + +sub(); +sub(); +sub(); + + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From bdfc1234ea30f9332e9d90618f2c4aa5edc9f4fa Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 15:47:36 -0700 Subject: [PATCH 07/10] closure funcitons --- assignments/closure.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 5eb3d872a..8f2a7868a 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -44,6 +44,18 @@ sub(); sub(); sub(); +function letsMultiply(x){ + return function(y){ + return x * y; + }; +} + +const mult666 = letsMultiply(666); +const mult13 = letsMultiply(13); + +console.log(mult666(13)); +console.log(mult13(666)); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From 72ef30084384dd86b13668e672ddbc3a067f3478 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 15:56:08 -0700 Subject: [PATCH 08/10] challenge 2 on closure --- assignments/closure.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 8f2a7868a..0a13cd372 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -68,11 +68,27 @@ const counterMaker = () => { // 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. + + let count = 0; + + function counter() { + return (count +=1); + } + counter(); + console.log(count); + counter(); + console.log(count); + counter(); + console.log(count); }; + +console.log(counterMaker()); + // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2 + // ==== Challenge 3: Make `counterMaker` more sophisticated ==== // It should have a `limit` parameter. Any counters we make with `counterMaker` // will refuse to go over the limit, and start back at 1. From 1c106c2dbc56c5fd1359cfca2dc7a51f85e0a96d Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 16:01:48 -0700 Subject: [PATCH 09/10] did challenge 4 on closure --- assignments/closure.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 0a13cd372..05fcd1c13 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -98,4 +98,20 @@ const 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 count = 0; + + function increment(){ + return (count += 1); + } + function decrement(){ + return (count -= 1); + } + + increment(); + increment(); + increment(); + decrement(); + console.log(count); }; + +console.log(counterFactory()); \ No newline at end of file From 6ac13c5b61025a3b89bfab7271305ade9c09e899 Mon Sep 17 00:00:00 2001 From: Lexie Jiang Date: Tue, 15 Oct 2019 16:02:13 -0700 Subject: [PATCH 10/10] edits --- assignments/closure.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 05fcd1c13..8950cf09e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -93,6 +93,8 @@ console.log(counterMaker()); // It should have a `limit` parameter. Any counters we make with `counterMaker` // will refuse to go over the limit, and start back at 1. + + // ==== 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`.