From 1322837443cfb7ec93ee6a5e92839a9f036bf5c4 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 21 May 2018 15:51:39 -0400 Subject: [PATCH 1/7] Array challenges completed. --- assignments/arrays.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/assignments/arrays.js b/assignments/arrays.js index c007f3e99..662c4e8ca 100644 --- a/assignments/arrays.js +++ b/assignments/arrays.js @@ -63,34 +63,54 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year" // ==== Challenge 1 ==== // The dealer can't recall the information for a car with an id of 33 on his lot. Help the dealer find out which car has an id of 33 by logging the car's year, make, and model in the console log provided to you below: -console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*` ); +for(let i = 0; i < inventory.length; i++) { + if (inventory[i].id === 33) { + console.log(`Car 33 is a ${inventory[i].car_year} ${inventory[i].car_make} ${inventory[i].car_model}` ); + } +} // ==== Challenge 2 ==== // The dealer needs the information on the last car in their inventory. What is the make and model of the last car in the inventory? Log the make and model into the console. -let lastCar = 0; -console.log(); +let lastCar = inventory[inventory.length - 1]; +console.log(`${lastCar.car_make} ${lastCar.car_model}`); // ==== Challenge 3 ==== // The marketing team wants the car models listed alphabetically on the website. Sort all the car model names into alphabetical order and log the results in the console let carModels = []; -console.log(); +for(let i = 0; i < inventory.length; i++) { + carModels.push(inventory[i].car_model); +} +console.log(carModels.sort()); // ==== Challenge 4 ==== // The accounting team needs all the years from every car on the lot. Create a new array from the dealer data containing only the car years and log the result in the console. let carYears = []; -console.log(); +for(let i = 0; i < inventory.length; i++) { + carYears.push(inventory[i].car_year); +} +console.log(carYears); // ==== Challenge 5 ==== // The car lot manager needs to find out how many cars are older than the year 2000. Using the carYears array you just created, find out how many cars were made before the year 2000 by populating the array oldCars and logging it's length. -let oldCars =[]; -console.log(); +let oldCars = []; +for(let i = 0; i < carYears.length; i++) { + if(carYears[i] < 2000) { + oldCars.push(carYears[i]); + } +} +console.log(oldCars); // ==== Challenge 6 ==== // A buyer is interested in seeing only BMW and Audi cars within the inventory. Return an array that only contains BMW and Audi cars. Once you have populated the BMWAndAudi array, use JSON.stringify() to show the results of the array in the console. let BMWAndAudi =[]; -console.log(); +for(let i = 0; i < inventory.length; i++) { + if(inventory[i].car_make === 'BMW' || inventory[i].car_make === 'Audi') { + BMWAndAudi.push(inventory[i]); + } +} +console.log(BMWAndAudi); From 3d9ee0d617cade0089c003d293a7b1f9771f5e79 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 21 May 2018 16:04:00 -0400 Subject: [PATCH 2/7] Finished objects.js except the stretch challenge --- assignments/objects.js | 55 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/assignments/objects.js b/assignments/objects.js index 04399eda9..6fd0b9483 100644 --- a/assignments/objects.js +++ b/assignments/objects.js @@ -19,26 +19,63 @@ let example = { // Write your intern objects here: - +const mitzi = { + "id": 1, + "name": "Mitzi", + "email": "mmelloy0@psu.edu", + "gender": "F", +}; + +const kennan = { + "id": 2, + "name": "Kennan", + "email": "kdiben1@tinypic.com", + "gender": "M", + "speak": function () { + return `Hello, my name is ` + this.name; + } +}; +const keven = { + "id": 3, + "name": "Keven", + "email": "kmummery2@wikimedia.org", + "gender": "M", +}; +const gannie = { + "id": 4, + "name": "Gannie", + "email": "gmartinson3@illinois.edu", + "gender": "M" +}; + +const antonietta = { + "id": 5, + "name": "Antonietta", + "email": "adaine5@samsung.com", + "gender": "F", + "multiplyNums": function (num1, num2) { + return num1 * num2; + } +}; // ==== Challenge 2: Reading Object Data ==== // Once your objects are created, log out the following requests from HR into the console: // Mitzi's name - +console.log(mitzi.name); // Kennan's ID - +console.log(kennan.id); // Keven's email - +console.log(keven.email); // Gannie's name - +console.log(gannie.name); // Antonietta's Gender - +console.log(antonietta.gender); // ==== Challenge 3: Object Methods ==== // Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided as a hint. -// console.log(kennan.speak()); +console.log(kennan.speak()); // Antonietta loves math, give her the ability to multiply two numbers together and return the product. Use the console.log provided as a hint. -//console.log(antonietta.multiplyNums(3,4)); +console.log(antonietta.multiplyNums(3,4)); // === Great work! === Head over to the the arrays.js file or take a look at the stretch challenge @@ -61,4 +98,4 @@ let parent = {} // Have the child speak -// Have the grandchild speak +// Have the grandchild speak \ No newline at end of file From d5be741d173314bb4a347c7139a12160eee53931 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 21 May 2018 16:13:36 -0400 Subject: [PATCH 3/7] Completed the stretch challenges in objects --- assignments/objects.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/assignments/objects.js b/assignments/objects.js index 6fd0b9483..bd0b9c20f 100644 --- a/assignments/objects.js +++ b/assignments/objects.js @@ -86,16 +86,38 @@ console.log(antonietta.multiplyNums(3,4)); // 3. Nest a grandchild object in the child object with properties for name and age. The name will be Sam and the age will be 30 // 4. Give each of the objects the ability to speak their names using the this keyword. -let parent = {} +let parent = { + "name": "Susan", + "age": 70, + "speak": function() { + return "My name is " + this.name; + }, + "child": { + "name": "George", + "age": 50, + "speak": function() { + return "My name is " + this.name; + }, + "grandchild": { + "name": "Sam", + "age": 30, + "speak": function() { + return "My name is " + this.name; + }, + } + } +} // Log the parent object's name - +console.log(parent.name); // Log the child's age - +console.log(parent.child.age); // Log the name and age of the grandchild - +console.log(parent.child.grandchild.name, parent.child.grandchild.age); // Have the parent speak - +console.log(parent.speak()); // Have the child speak +console.log(parent.child.speak()); -// Have the grandchild speak \ No newline at end of file +// Have the grandchild speak +console.log(parent.child.grandchild.speak()); From 25f0553f5aa12f099df33ac35a165ad0d575fd60 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 21 May 2018 16:39:04 -0400 Subject: [PATCH 4/7] Added the JSON.stringify() method in challenge 6 --- assignments/arrays.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/arrays.js b/assignments/arrays.js index 662c4e8ca..3240d1eb1 100644 --- a/assignments/arrays.js +++ b/assignments/arrays.js @@ -110,7 +110,7 @@ for(let i = 0; i < inventory.length; i++) { BMWAndAudi.push(inventory[i]); } } -console.log(BMWAndAudi); +console.log(JSON.stringify(BMWAndAudi)); From a575d6f95f08282cc0a7c20ce1324125e8c62878 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 22 May 2018 14:25:07 -0400 Subject: [PATCH 5/7] Completed callback assignment excluding stretch goal --- assignments/callbacks.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index a551f853b..64db8a7be 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,28 +2,37 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. + cb(arr[0]); } function getLength(arr, cb) { // getLength passes the length of the array into the callback. + cb(arr.length); } function last(arr, cb) { // last passes the last item of the array into the callback. + cb(arr[arr.length-1]) } function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); } function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); } 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. + const exists = list.includes(item); + return cb(exists); } +console.log(contains(5,[3,4], isTrue => `I am ${isTrue}`)); +console.log(contains(5,[3,4,5], isTrue => `I am ${isTrue}`)); /* STRETCH PROBLEM */ From 1d73d2a7d31d9744dcedff3edf7caeba482ccf68 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 22 May 2018 14:30:29 -0400 Subject: [PATCH 6/7] Completed stretch goal on callbacks --- assignments/callbacks.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 64db8a7be..decb1f43b 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -40,4 +40,14 @@ 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. + const uniqueArr = []; + array.forEach(item => { + if(!uniqueArr.includes(item)) { + uniqueArr.push(item); + } + }); + + return cb(uniqueArr); } + +console.log(removeDuplicates([5,2,3,4,5,2], arr => arr)); \ No newline at end of file From 2cb7151d8333f01977b389776e861a158d4234e8 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 22 May 2018 14:31:42 -0400 Subject: [PATCH 7/7] Fixed Challenge 5 in arrays.js --- assignments/arrays.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/arrays.js b/assignments/arrays.js index 3240d1eb1..a4c75e42c 100644 --- a/assignments/arrays.js +++ b/assignments/arrays.js @@ -100,7 +100,7 @@ for(let i = 0; i < carYears.length; i++) { oldCars.push(carYears[i]); } } -console.log(oldCars); +console.log(oldCars.length); // ==== Challenge 6 ==== // A buyer is interested in seeing only BMW and Audi cars within the inventory. Return an array that only contains BMW and Audi cars. Once you have populated the BMWAndAudi array, use JSON.stringify() to show the results of the array in the console.