diff --git a/challenges/functions.js b/challenges/functions.js index 6e3688bfcc..2acbfa18fc 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -6,26 +6,38 @@ * 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!" */ +const add = (a, b) =>{ + return a + b; +} +const multiply = (a, b) => { + return a * b; +} + +const greeting = (a, b) =>{ + return `Hello ${a} ${b}, 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! +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: +// Explanation: Because it is iuherited, variables can be passed down (not up). const external = "I'm outside the function"; diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index 659e3e149c..6ea9dcf137 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -7,28 +7,50 @@ */ // tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous - +const dinosaurOne ={ + name: 'tyrannosaurus', + diet: 'carnivorous', + weight: '7000kg', + length: '12m', + period: 'Late Cretaceous', + //function === methods + roar: function(){ + return '"RAWERSRARARWERSARARARRRR!"'; + }, +}; // stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic - +const dinosaurTwo ={ + name: 'stegosaurus', + diet: 'herbivorous', + weight: '2000kg', + length: '9m', + period: 'Late Jurassic' + }; // velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceous - +const dinosaurThree ={ + name: 'velociraptor', + diet: 'carnivorous', + weight: '15kg', + length: '1.8m', + period: 'Late Cretaceous' + }; // Using your dinosaur objects, log answers to these questions: // How much did tyrannosaurus weigh? -console.log(); +console.log(dinosaurOne.weight); // What was the diet of a velociraptor? -console.log(); +console.log(dinosaurThree.diet); // How long was a stegosaurus? -console.log(); +console.log(dinosaurTwo.length); // What time period did tyrannosaurus live in? -console.log(); +console.log(dinosaurOne.period); // Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result. -console.log(); +console.log(dinosaurOne.roar()); // ==== Arrays ==== @@ -51,7 +73,20 @@ const graduates = [ /* 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, sort the universities alphabetically and log the result. */ -const universities = []; +const universities = graduates[graduates.university] + +function sortUniversities(universities) { + universities.sort(function(university1, university2){ + if ( university1.university < university2.university ){ + return -1 + } + if (university1.university > university2.university){ + return 1 + } + return 0 + }) + return 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. @@ -60,7 +95,7 @@ The resulting contact information strings should have a space between the first "Josh josh@example.com" Log the result of your new array. */ -const contactInfo = []; +const contactInfo = graduates[first_name, email]; 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. */