diff --git a/index.js b/index.js index 00027a1c8..d7bbaed5c 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ //🚀🚀🚀 Topic #1 Closures 🚀🚀🚀// /* 🚀🚀🚀🤓 Task 1: 🤓🚀🚀🚀 Study the code below and explain in your own words why nested function can access the variable internal. */ +// Because nestedFunction is being invoked inside myFunction and due to lexical scoping, any function inside another function has access to its variables. const external = "I'm outside the function"; @@ -15,11 +16,7 @@ function myFunction() { } //myFunction(); -//🚀🚀🚀 ⬇️ 📝 Explanation ⬇️ 📝 🚀🚀🚀: - - - - +//🚀🚀🚀 ⬇️ 📝 Explanation ⬇️ 📝 🚀🚀🚀: /* 🚀🚀🚀 Task 2: Counter 🚀🚀🚀 */ /* Use summation to do the following: @@ -30,29 +27,82 @@ function myFunction() { 💡 NOTE: you may use a for loop for this function if you wish */ -function summation(/*Your Code Here*/) { +function summation(num) { /*Your Code Here*/ - + if (num === 1) { + return 1; + } else { + return num + summation(num - 1); } - +} // 🦁🦁🦁 Topic 2: 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() 🦁🦁🦁 + { + 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. 1. Receive the zooAnimals array as an argument passed from a parameter 2. Use .forEach() to populate a new array called displayNames that will be an array of strings with only the animal name and scientific name of each animal @@ -60,12 +110,20 @@ const zooAnimals = [ 💡 NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}" */ - function animalNames(/*Your Code Here*/){ - /*Your Code Here*/ - } - +function animalNames(arr) { + /*Your Code Here*/ + const displayNames = []; + arr.forEach((animal) => { + return displayNames.push( + `name: ${animal.animal_name}, scientific: ${animal.scientific_name}` + ); + }); + return displayNames; +} + +console.log(animalNames(zooAnimals)); - /* 🦁🦁🦁 Request 2: .map() 🦁🦁🦁 +/* 🦁🦁🦁 Request 2: .map() 🦁🦁🦁 The zoo needs a list of all their animal's names converted to lower case. Use lowerCaseNames to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -75,12 +133,15 @@ const zooAnimals = [ 💡 NOTE: Do some research for other methods that can help help you */ - function lowerCaseNames(/*Your Code Here*/){ - /*Your Code Here*/ - } - - - /* 🦁🦁🦁 Request 3: .filter() 🦁🦁🦁 +function lowerCaseNames(arr) { + /*Your Code Here*/ + const lowerCaseAnimals = arr.map((animal) => { + return animal.animal_name.toLowerCase(); + }); + return lowerCaseAnimals; +} + +/* 🦁🦁🦁 Request 3: .filter() 🦁🦁🦁 The zoo is concerned about animals with a lower population count. Use lowPopulationAnimals to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -88,12 +149,15 @@ const zooAnimals = [ 3. Return this new array */ - function lowPopulationAnimals(/*Your Code Here*/){ - /*Your Code Here*/ - } - +function lowPopulationAnimals(arr) { + /*Your Code Here*/ + const lowCount = arr.filter((animal) => { + return animal.population < 5; + }); + return lowCount; +} - /* 🦁🦁🦁 Request 4: .reduce() 🦁🦁🦁 +/* 🦁🦁🦁 Request 4: .reduce() 🦁🦁🦁 The zoo needs to know their total animal population across the United States. USe USApop to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -102,13 +166,16 @@ const zooAnimals = [ 💡 NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax! */ - function USApop(/*Your Code Here*/){ - /*Your Code Here*/ - } - - - // 🦁🦁🦁 Callbacks 🦁🦁🦁 - /* 🦁🦁🦁 Step 1: Create a higher-order function 🦁🦁🦁 +function USApop(arr) { + /*Your Code Here*/ + const total = arr.reduce((acc, currVal) => { + return (acc += currVal.population); + }, 0); + return total; +} + +// 🦁🦁🦁 Callbacks 🦁🦁🦁 +/* 🦁🦁🦁 Step 1: Create a higher-order function 🦁🦁🦁 Use the higher-order function called consume to do the following: 1. Receive 3 parameters: a, b and cb. The first two parameters (a and b) can take any argument (we can pass any value as an argument) and the last parameter (cb) accepts a callback 2. Return the invocation of cb taking `a` and `b` as its arguments @@ -116,51 +183,48 @@ const zooAnimals = [ 💡 NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2. */ - function consume(/*Your Code Here */){ - /*Your Code Here */ - } - - - // 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 +function consume(a, b, cb) { + /*Your Code Here */ + return cb(a, b); +} + +// 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 - /* Use add to do the following: +/* Use add to do the following: 1. Receive two numbers as an argument that are passed in from its first and second parameters 2. Return the sum of those numbers */ -function add(/*Your Code Here */){ - /*Your Code Here*/ - } - +function add(n1, n2) { + /*Your Code Here*/ + return n1 + n2; +} /* Use multiply to do the following: 1. Receive two numbers as an argument that are passed in from its first and second parameters 2. Return the product of those numbers */ -function multiply(/*Your Code Here */){ - /*Your Code Here */ - } - +function multiply(n1, n2) { + /*Your Code Here */ + return n1 * n2; +} - /* Use greeting to do the following: +/* Use greeting to do the following: 1. Receive two strings (a first name and last name) as an argument that are passed in from its first and second parameters 2. Return "Hello {first-name} {last-name}, nice to meet you!" 💡 NOTE: The string returned must match the format above or the test will not pass! */ -function greeting(/*Your Code Here */){ - return /*Your Code Here */ - } - - -// 🦁🦁🦁 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! - +function greeting(firstName, lastName) { + return `Hello ${firstName} ${lastName}, 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! // 🐴🐴🐴 Topic 3: Prototypes 🐴🐴🐴 // @@ -175,79 +239,92 @@ function greeting(/*Your Code Here */){ - Instances of CuboidMaker should initialize `length`, `width` and `height` properties */ -function CuboidMaker(/*Your Code Here */){ +function CuboidMaker(attr) { /*Your Code Here */ + this.length = attr.length; + this.width = attr.width; + this.height = attr.height; } - /* 🐴🐴🐴 Step 2: Volume Method 🐴🐴🐴 Create a method called volume using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height 💡 NOTE: 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 called surfaceArea using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. 💡 NOTE: Formula for cuboid surface area: 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 (not auto graded)🐴🐴🐴 Create an object called cuboid 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(4, 5, 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 // 🦄🦄🦄 Topic 4: Classes 🦄🦄🦄 // //Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class. - -class CuboidMakerTwo{ +class CuboidMakerTwo { + 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 cuboidTwo = new CuboidMakerTwo(4, 5, 5); //🦄🦄🦄 Test your volume and surfaceArea methods by uncommenting the logs below: 🦄🦄🦄 // console.log(cuboidTwo.volume()); // 100 // console.log(cuboidTwo.surfaceArea()); // 130 - - - - - - - - /* 🛑🛑🛑🛑🛑 Please do not modify anything below this line 🛑🛑🛑🛑🛑 */ - function foo(){ - console.log('its working'); - return 'bar'; - } - foo(); - module.exports = { - foo, - summation, - animalNames, - lowerCaseNames, - lowPopulationAnimals, - USApop, - consume, - add, - multiply, - greeting, - CuboidMaker, - CuboidMakerTwo - } +/* 🛑🛑🛑🛑🛑 Please do not modify anything below this line 🛑🛑🛑🛑🛑 */ +function foo() { + console.log('its working'); + return 'bar'; +} +foo(); +module.exports = { + foo, + summation, + animalNames, + lowerCaseNames, + lowPopulationAnimals, + USApop, + consume, + add, + multiply, + greeting, + CuboidMaker, + CuboidMakerTwo, +};