diff --git a/README.md b/README.md index a28a9c5f6..80d74f21c 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ With some basic JavaScript principles in hand, we can now expand our skills out ## Task 1: Set Up The Project With Git -* [ ] Fork the project into your GitHub user account -* [ ] Clone the forked project into a directory on your machine -* [ ] You are now ready to build this project with your preferred IDE -* [ ] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console. +* [X] Fork the project into your GitHub user account +* [X] Clone the forked project into a directory on your machine +* [X] You are now ready to build this project with your preferred IDE +* [X] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console. ## Task 2: Callbacks @@ -43,6 +43,6 @@ We have learned that closures allow us to access values in scope that have alrea ## Stretch Goals -* [ ] Arrow Function Syntax - [Check out this awesome guide for ES6 arrow syntax](https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26). You will see more and more arrow functions as you progress deeper into JavaScript. Use the [stretch-function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax. +* [ ] Arrow Function Syntax - [Check out this awesome guide for ES6 arrow syntax](https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26). You will see more and more arrow functions as you progress deeper into JavaScript. Use the [stretch-function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax. -* [ ] Look up what an IIFE is in JavaScript and experiment with them \ No newline at end of file +* [ ] Look up what an IIFE is in JavaScript and experiment with them diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..4dbf7608f 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -54,23 +54,44 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c {"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; // ==== 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 into a new array called fullName. +// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName. let fullName = []; + +runners.forEach(runner => { + let combinedFirstAndLast = runner.first_name + ' ' + runner.last_name; + fullName.push(combinedFirstAndLast); +}); + console.log(fullName); + // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result let allCaps = []; -console.log(allCaps); + +allCaps = runners.map(runner => { + return runner.first_name.toUpperCase() +}) +console.log(allCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result let largeShirts = []; + +largeShirts = runners.filter(runner => { + if (runner.shirt_size === "L"){ + return true; + } +}) console.log(largeShirts); // ==== Challenge 4: Use .reduce() ==== // The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result let ticketPriceTotal = []; + +ticketPriceTotal = runners.reduce(function(accumulator, currentValue){ + return accumulator + currentValue.donation; +}, 0) console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== @@ -78,6 +99,53 @@ console.log(ticketPriceTotal); // Problem 1 +// The company needs to get all the emails in order to do a marketing campaign for their next run. +let emails = []; + +runners.forEach(runner => { + let email = runner.email; + emails.push(email); +}); + +console.log(emails); + // Problem 2 -// Problem 3 \ No newline at end of file +// Before the competition, a local store sponsor held a giveaway for the attendees and Anderea from Kwimbee won. The store has to verify that the person actually attended the race in order to provide the price. + +let winner = []; +winner = runners.filter(runner => { + if (runner.company_name === "Kwimbee" && runner.first_name === "Anderea"){ + return true; + } else return false; +}) + +console.log(winner) + +// Problem 3 +// The local community center wants to honor the top 10 donators for their contribution. Provide a list of the highest donators. + +// name and donation: +let nameAndDonations = [] + +runners.forEach(runner => { + let nameAndDonation = [] + let name = runner.first_name + ' ' + runner.last_name; + let donation = runner.donation; + nameAndDonation.push(name,donation) + nameAndDonations.push(nameAndDonation); +}); + +nameAndDonations.sort(function(a,b){ + return b[1] - a[1]; +}); + +let topTen = [] +topTen = nameAndDonations.slice(0,10); + +console.log(topTen); + + + + +console.log(emails); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..897f8a90c 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,10 +2,10 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* +/* + + //Given this problem: - //Given this problem: - function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. } @@ -24,24 +24,60 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr.length); } +getLength(items, function(length){ + console.log(length); +}) + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length -1]); } +last(items, function(last){ + console.log(last); +}) + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return (cb(x + y)); } +// regular function +sumNums(2,3, function(sum){ + console.log(sum); +}) + // ES6 double arrow +sumNums(3,4, sum => console.log(sum)); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return (cb(x * y)); } +multiplyNums(3,4, function(multiply){ + console.log(multiply) +}) + +//ES6 double Arrow +multiplyNums(5,12, multiply => console.log(multiply)); + 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. -} + // contains checks if an item is present inside of the given array/list. + // Pass true to the callback if it is, otherwise pass false. + return (cb(item, list)) + } + function checkList(item, list){ + if (list.includes(item)){ + return true; + } else return false + } + console.log(contains("Pencil", items, checkList)) + console.log(contains("hi", items, checkList)) + /* STRETCH PROBLEM */ @@ -49,4 +85,21 @@ 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. + return cb(array) } + +function removeDuplicate(array){ + let newArray = []; + // .slice() returns a shallow copy of a portion of an arra into a new array object. original array will not be modified + let sortedArray = array.slice().sort(); + for (i=0; i< sortedArray.length; i++){ + if (sortedArray[i] != sortedArray[i-1]){ + newArray.push(sortedArray[i]) + } + } + return newArray; +} + +exampleArray = ['Con', 'Hi', 'Hello', 'bye', 'Hi', 'Hi'] + +console.log(removeDuplicates(exampleArray, removeDuplicate)); diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..5eddea3e8 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,20 +1,52 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +const multiplyByFour = () => { + const four = 4 + showAnswer = () => { + console.log(four * 3); + } + showAnswer() +} + +multiplyByFour() + // ==== Challenge 2: Create a counter function ==== const counter = () => { + let count = 1; + + return function(){ + return count++; + }; // Return a function that when invoked increments and returns a counter variable. }; -// Example usage: const newCounter = counter(); -// newCounter(); // 1 -// newCounter(); // 2 +const newCounter = counter(); + +// Example usage: const newCounter = counter(); +console.log(newCounter()); // 1 +console.log(newCounter()); // 2 +console.log(newCounter()); /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== + +// 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. 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 counter = 0; + let increment = () => counter += 1; + let decrement = () => counter -= 1; + + let myFunctions = {increment, decrement}; + + return myFunctions; }; + +let newFactory = counterFactory() +console.log(newFactory.increment()) +console.log(newFactory.increment()) +console.log(newFactory.increment()) +console.log(newFactory.decrement())