Skip to content

Commit 6234990

Browse files
committed
Finished arrays and objects
1 parent 335d554 commit 6234990

2 files changed

Lines changed: 73 additions & 15 deletions

File tree

‎assignments/arrays.js‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,57 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year"
6363

6464
// ==== Challenge 1 ====
6565
// 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:
66-
console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*` );
66+
67+
for (i=0;i<inventory.length;i++){
68+
if (inventory[i].id == 33){
69+
str = "Car 33 is a " + inventory[i].car_make + " "+ inventory[i].car_model;
70+
console.log(str);
71+
}
72+
}
73+
6774

6875

6976

7077
// ==== Challenge 2 ====
7178
// 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.
72-
let lastCar = 0;
73-
console.log();
79+
let lastCar = inventory[inventory.length-1];
80+
console.log(lastCar.car_make);
81+
console.log(lastCar.car_model);
7482

7583
// ==== Challenge 3 ====
7684
// 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
7785
let carModels = [];
78-
console.log();
86+
for (i=0;i<inventory.length;i++){
87+
carModels.push(inventory[i].car_model);
88+
}
89+
console.log(carModels.sort());
7990

8091
// ==== Challenge 4 ====
8192
// 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.
8293
let carYears = [];
83-
console.log();
94+
for (i=0;i<inventory.length;i++){
95+
carYears.push(inventory[i].car_year);
96+
}
97+
console.log(carYears);
8498

8599
// ==== Challenge 5 ====
86100
// 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.
87101
let oldCars =[];
88-
console.log();
102+
let cnt =0;
103+
for (i=0;i<carYears.length;i++){
104+
if(carYears[i] < 2000) cnt++
105+
}
106+
console.log(cnt);
89107

90108
// ==== Challenge 6 ====
91109
// 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.
92110
let BMWAndAudi =[];
93-
console.log();
111+
for (i=0;i<inventory.length;i++){
112+
if (inventory[i].car_make == "Audi" || inventory[i].car_make == "BMW"){
113+
BMWAndAudi.push(inventory[i])
114+
}
115+
}
116+
console.log(JSON.stringify(BMWAndAudi));
94117

95118

96119

‎assignments/objects.js‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,48 @@ let example = {
1818
}
1919

2020
// Write your intern objects here:
21+
function Intern(id, email, first, gender){
22+
this.id = id;
23+
this.email = email;
24+
this.first = first;
25+
this.gender = gender;
26+
}
27+
28+
let mitzi = new Intern(1,'mmelloy0@psu.edu','Mitzi','F');
29+
let kennan = new Intern(2,'kdiben1@tinypic.com','Kennan','M');
30+
let keven = new Intern(3,'kmummery2@wikimedia.org','Keven','M');
31+
let gannie = new Intern(4,'gmartinson3@illinois.edu','Gannie','M');
32+
let antonietta = new Intern(5,'adaine5@samsung.com','Antonietta','F');
2133

2234

2335
// ==== Challenge 2: Reading Object Data ====
2436
// Once your objects are created, log out the following requests from HR into the console:
2537

2638
// Mitzi's name
27-
39+
console.log(mitzi.name);
2840
// Kennan's ID
29-
41+
console.log(kennan.id);
3042
// Keven's email
31-
43+
console.log(keven.email);
3244
// Gannie's name
33-
45+
console.log(gannie.name);
3446
// Antonietta's Gender
47+
console.log(antonietta.gender);
48+
3549

3650
// ==== Challenge 3: Object Methods ====
3751
// Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided as a hint.
38-
// console.log(kennan.speak());
52+
53+
kennan.speak = function(){
54+
return "Hello, my name is" + this.name;
55+
}
56+
console.log(kennan.speak());
3957

4058
// Antonietta loves math, give her the ability to multiply two numbers together and return the product. Use the console.log provided as a hint.
41-
//console.log(antonietta.multiplyNums(3,4));
59+
antonietta.multiplyNum = function(a,b){
60+
return a*b;
61+
}
62+
console.log(antonietta.multiplyNums(3,4));
4263

4364
// === Great work! === Head over to the the arrays.js file or take a look at the stretch challenge
4465

@@ -49,10 +70,24 @@ let example = {
4970
// 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
5071
// 4. Give each of the objects the ability to speak their names using the this keyword.
5172

52-
let parent = {}
73+
let parent = {
74+
name: 'Susan',
75+
age: 70,
76+
child: {
77+
name: George,
78+
age:50,
79+
child:{
80+
name:Sam,
81+
age:30,
82+
speak: function(){return "My Name is " + this.name;}
83+
},
84+
speak: function(){return "My Name is " + this.name;}
85+
},
86+
speak: function(){return "My Name is " + this.name;}
87+
}
5388

5489
// Log the parent object's name
55-
90+
console.log(parent.name);
5691
// Log the child's age
5792

5893
// Log the name and age of the grandchild

0 commit comments

Comments
 (0)