diff --git a/README.md b/README.md index 408612117c..792e24bb2b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Sprint Challenge: JavaScript Fundamentals -This challenge allows you to practice the concepts and techniques learned over the past week and apply them in a survey of problems. This Sprint explored JavaScript Fundamentals. During this Sprint, you studied variables, functions, object literals, arrays, this keyword, prototypes, and class syntax. In your challenge this week, you will demonstrate proficiency by completing a survey of JavaScript problems. +This challenge allows you to practice the concepts and techniques learned over the past week and apply them in a survey of problems. This Sprint explored JavaScript Fundamentals. During this Sprint, you studied array methods, this keyword, prototypes, and class syntax. In your challenge this week, you will demonstrate proficiency by completing a survey of JavaScript problems. ## Instructions @@ -10,11 +10,7 @@ This is an individual assessment. All work must be your own. Your challenge scor You are not allowed to collaborate during the Sprint Challenge. However, you are encouraged to follow the twenty-minute rule and seek support from your TL and Instructor in your cohort help channel on Slack. Your work reflects your proficiency in JavaScript fundamentals. -You have three hours to complete this challenge. Plan your time accordingly. - -## Commits - -Commit your code regularly and meaningfully. This helps both you (in case you ever need to return to old code for any number of reasons) and your team lead. +> You have three hours to complete this challenge. Plan your time accordingly. ## Description @@ -28,19 +24,36 @@ Demonstrate your understanding of this week's concepts by answering the followin Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your team lead -1. Describe the biggest difference between `.forEach` & `.map`. +1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) + +foreach has an action for each element for the current array in an array where as map creates a new array to take action on and change. + +2. Explain the difference between a callback and a higher order function. -2. What is the difference between a function and a method? +A callback goes in a HOF. 3. What is closure? +A closed enviorment of code in whic things may come in but never leave. + 4. Describe the four rules of the 'this' keyword. +Global Binding: Defaults to refer to othe window. + +Impliscit Binding: Only applies to objects with methods. + +New Binding: Uses constructive functions to create new things + +Exsplicit Binding: Overrides other bindings by using .call and .apply etc + 5. Why do we need super() in an extended class? -## Project Set up +It will not function without it. + +### Task 1 - Project Set up Follow these steps to set up and work on your project: +Make sure you clone the branch that the TK links to: the vnext branch, NOT master! - [ ] Create a forked copy of this project. - [ ] Add TL as collaborator on Github. @@ -51,37 +64,44 @@ Follow these steps to set up and work on your project: - [ ] Implement the project on your Branch, committing changes regularly. - [ ] Push commits: git push origin ``. -Follow these steps for completing your project: - -- [ ] Submit a Pull-Request to merge Branch into master (student's Repo). -- [ ] Add your team lead as a Reviewer on the Pull-request -- [ ] TL then will count the HW as done by merging the branch back into master. -## Minimum Viable Product +### Task 2 - Minimum Viable Product Your finished project must include all of the following requirements: **Pro tip for this challenge: If something seems like it isn't working locally, copy and paste your code up to codepen and take another look at the console.** -## Task 1: Objects and Arrays -Test your knowledge of objects and arrays. -* [ ] Use the [objects-arrays.js](challenges/objects-arrays.js) link to get started. Read the instructions carefully! +#### Task A: Objects and Arrays + +Test your knowledge of advanced array methods and callbacks. +* [ ] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully! + +#### Task B: Closure -## Task 2: Functions -This challenge takes a look at callbacks and closures as well as scope. -* [ ] Use the [functions.js](challenges/functions.js) link to get started. Read the instructions carefully! +This challenge takes a look at closures as well as scope. +* [ ] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully! + +#### Task C: Prototypes -## Task 3: Prototypes Create constructors, bind methods, and create cuboids in this prototypes challenge. * [ ] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully! -## Task 4: Classes +#### Task D: Classes + Once you have completed the prototypes challenge, it's time to convert all your hard work into classes. * [ ] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully! In your solutions, it is essential that you follow best practices and produce clean and professional results. Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work. It is better to submit a challenge that meets MVP than one that attempts too much and does not. -## Stretch Problems +### Task 3 - Stretch Problems There are a few stretch problems found throughout the files, don't work on them until you are finished with MVP requirements! + +## Submission Format + +Follow these steps for completing your project: + +- [ ] Submit a Pull-Request to merge Branch into master (student's Repo). +- [ ] Add your team lead as a Reviewer on the Pull-request +- [ ] TL then will count the HW as done by merging the branch back into master. diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js new file mode 100644 index 0000000000..ea4d25d599 --- /dev/null +++ b/challenges/arrays-callbacks.js @@ -0,0 +1,97 @@ +// ==== ADVANCED Array Methods ==== + +// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems. + +const zooAnimals = [ + { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, + { animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" }, + { animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" }, + { animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" }, + { animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" }, + { animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" }, + { animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" }, + { animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" }, + { animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" }, + { animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" }, +]; + +/* Request 1: .forEach() + +The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." + +*/ +const displayNames = []; +zooAnimals.forEach(element => { + displayNames.push(`Name: ${element.animal_name}, Sientific: ${element.scientific_name}`) +}); +console.log(displayNames); + +/* Request 2: .map() + +The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut. + +*/ + +const lowCaseAnimalNames = zooAnimals.map(name => name.animal_name.toLowerCase());; +console.log(lowCaseAnimalNames); + +/* Request 3: .filter() + +The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5. + +*/ +const lowPopulationAnimals = zooAnimals.filter(function (pop){return pop.population < 5;}); +console.log(lowPopulationAnimals); + +/* Request 4: .reduce() + +The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count. + +*/ +const populationTotal = zooAnimals.reduce((total, pop) => { + return total += pop.population; +}, 0); +console.log(populationTotal); + + +// ==== Callbacks ==== + +/* Step 1: Create a higher-order function + * Create a higher-order function named consume with 3 parameters: a, b and cb + * The first two parameters can take any argument (we can pass any value as argument) + * The last parameter accepts a callback + * The consume function should return the invocation of cb, passing a and b into cb as arguments +*/ +function consume(a,b,cb){ + return cb( a , b ); +} + +/* Step 2: Create several functions to callback with consume(); + * Create a function named add that returns the sum of two numbers + * Create a function named multiply that returns the product of two numbers + * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" +*/ +function add(a,b){ + return a + b; +} +function multiply(a,b){ + return a*b; +} +function greeting(first_name, last_name){ + return `Hello ${first_name} ${last_name}, nice to meet you!`; +} + +/* Step 3: Check your work by un-commenting the following calls to consume(): */ +console.log(consume(2, 2, add)); // 4 +console.log(consume(10, 16, multiply)); // 160 +console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! + + + + +/* + +Stretch: If you haven't already, convert your array method callbacks into arrow functions. + +*/ + diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..9e7e05780c 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,23 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker{ + constructor(attr){ + this.length = attr.length; + this.width = attr.width; + this.height = attr.height;} + volume(){ + return this.length * this.width * this.height; + } + + surfaceArea(){ + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); + } + } + + + const cuboid = new CuboidMaker({length: 4, width: 5, height: 5}); // Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +console.log(cuboid.volume()); // 100 +console.log(cuboid.surfaceArea()); // 130 // Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area. \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js new file mode 100644 index 0000000000..b02af84eb3 --- /dev/null +++ b/challenges/closure.js @@ -0,0 +1,29 @@ +// ==== Closures ==== + +/* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ + + +const external = "I'm outside the function"; + +function myFunction() { + console.log(external); + const internal = "Hello! I'm inside myFunction!"; + + function nestedFunction() { + console.log(internal); + }; + nestedFunction(); +} +myFunction(); + +// Explanation: +/* It can access it becuase it has the right to inherient its + parents assets */ + +/* Task 2: Counter */ + +/* Create a function called `sumation` that accepts a parameter and uses a counter to return the summation of that number. For example, `summation(4)` should return 10 because 1+2+3+4 is 10. */ +function sumation(num){ + return (num*(num+1)/2); +} +console.log(sumation(4)) diff --git a/challenges/functions.js b/challenges/functions.js deleted file mode 100644 index 6e3688bfcc..0000000000 --- a/challenges/functions.js +++ /dev/null @@ -1,42 +0,0 @@ -// ==== Callbacks ==== - -/* Step 1: Create a higher-order function - * Create a higher-order function named consume with 3 parameters: a, b and cb - * The first two parameters can take any argument (we can pass any value as argument) - * The last parameter accepts a callback - * The consume function should return the invocation of cb, passing a and b into cb as arguments -*/ - - -/* Step 2: Create several functions to callback with consume(); - * Create a function named add that returns the sum of two numbers - * Create a function named multiply that returns the product of two numbers - * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" -*/ - - -/* Step 3: Check your work by un-commenting the following calls to consume(): */ -// console.log(consume(2, 2, add)); // 4 -// console.log(consume(10, 16, multiply)); // 160 -// console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! - - -// ==== Closures ==== - -// Explain in your own words why nestedfunction can access the variable internal. - -// Explanation: - - -const external = "I'm outside the function"; - -function myFunction() { - console.log(external); - const internal = "Hello! I'm inside myFunction!"; - - function nestedFunction() { - console.log(internal); - }; - nestedFunction(); -} -myFunction(); diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..0dc5ec27dd 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,8 +7,8 @@ Sprint Challenge - - + + diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js deleted file mode 100644 index 86970b0c29..0000000000 --- a/challenges/objects-arrays.js +++ /dev/null @@ -1,127 +0,0 @@ -// ==== Objects ==== - -/* - Given the following information about dinosaurs, create 3 objects: - Use this pattern to create your objects: - object name, diet, weight, length, period -*/ - -// tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous - -// stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic - -// velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceous - -// Using your dinosaur objects, log answers to these questions: - -// How much did tyrannosaurus weigh? -console.log(); - -// What was the diet of a velociraptor? -console.log(); - -// How long was a stegosaurus? -console.log(); - -// What time period did tyrannosaurus live in? -console.log(); - - -// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result. -console.log(); - - -// ==== Arrays ==== - -// Given an array of college graduates. Complete the following requests using any array method you like - -const graduates = [ - { id: 1, first_name: "Cynde", university: "Missouri Southern State College", email: "ctorry0@macromedia.com" }, - { id: 2, first_name: "Saundra", university: "The School of the Art Institute of Chicago", email: "swhal1@state.gov" }, - { id: 3, first_name: "Lambert", university: "Marian College", email: "lparham2@techcrunch.com" }, - { id: 4, first_name: "Modestine", university: "International Medical & Technological University", email: "mdolder3@symantec.com" }, - { id: 5, first_name: "Chick", university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic", email: "camorts4@google.com.au" }, - { id: 6, first_name: "Jakob", university: "Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft", email: "jharken5@spiegel.de" }, - { id: 7, first_name: "Robbi", university: "Salem University", email: "rbrister6@redcross.org" }, - { id: 8, first_name: "Colline", university: "Coastal Carolina University", email: "cbrosh7@alibaba.com" }, - { id: 9, first_name: "Michail", university: "Universidad Católica de Ávila", email: "mrome8@shinystat.com" }, - { id: 10, first_name: "Hube", university: "Universitat Rovira I Virgili Tarragona", email: "hlethbrig9@foxnews.com" }, -]; - -/* Request 1: Create a new array called universities that contains all the universities in the graduates array. This will be an array of strings. - -Once you have the new array created, log the result. */ -const universities = []; -console.log(universities); - -/* Request 2: Create a new array called contactInfo that contains both first name and email of each student. This will be an array of strings. - -The resulting contact information strings should have a space between the first name and the email, like this: -"Josh josh@example.com" - -Log the result of your new array. */ -const contactInfo = []; -console.log(contactInfo); - -/* Request 3: Find out how many universities have the string "Uni" included in their name. Create a new array called unisWithUni that contains them all. This will be an array of objects. Log the result. */ -const unisWithUni = []; -console.log(unisWithUni); - - -// ==== ADVANCED Array Methods ==== - -// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems. - -const zooAnimals = [ - { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, - { animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" }, - { animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" }, - { animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" }, - { animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" }, - { animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" }, - { animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" }, - { animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" }, - { animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" }, - { animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" }, -]; - -/* Request 1: .forEach() - -The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." - -*/ -const displayNames = []; -console.log(displayNames); - -/* Request 2: .map() - -The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut. - -*/ - -const lowCaseAnimalNames = []; -console.log(lowCaseAnimalNames); - -/* Request 3: .filter() - -The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5. - -*/ -const lowPopulationAnimals = []; -console.log(lowPopulationAnimals); - -/* Request 4: .reduce() - -The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count. - -*/ -const populationTotal = 0; -console.log(populationTotal); - - -/* - -Stretch: If you haven't already, convert your array method callbacks into arrow functions. - -*/ - diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..a7574e6d40 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,29 +5,38 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ - +function CuboidMaker(attr){ + this.length = attr.length; + this.width = attr.width; + this.height = attr.height; +} /* == Step 2: Volume Method == Create a method using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height Formula for cuboid volume: length * width * height */ - +CuboidMaker.prototype.volume = function(){ + return this.length * this.width * this.height; +} /* == Step 3: Surface Area Method == Create another method using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ - +CuboidMaker.prototype.surfaceArea = function(){ + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) +} /* == Step 4: Create a new object that uses CuboidMaker == Create a cuboid object that uses the new keyword to use our CuboidMaker constructor Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ +const cuboid = new CuboidMaker({length: 4, width: 5, height: 5}); // Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +console.log(cuboid.volume()); // 100 +console.log(cuboid.surfaceArea()); // 130