Skip to content

Commit 66fa0f4

Browse files
authored
Merge pull request #1 from aapetsi/apetsi-ampiah
Apetsi ampiah
2 parents d5d5a4a + bbf60c0 commit 66fa0f4

File tree

3 files changed

+160
-20
lines changed

3 files changed

+160
-20
lines changed

assignments/arrays.js

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,93 @@ let inventory = [
7777
// 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:
7878
console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*`);
7979

80+
for (let i = 0; i < inventory.length; i++) {
81+
if (inventory[i].id === 33) {
82+
console.log(`Carr 33 is a ${inventory[i].car_year} ${inventory[i].car_make} ${inventory[i].car_model}`)
83+
}
84+
}
85+
86+
// Stretch for challenge 1
87+
inventory.forEach(car => {
88+
if (car.id === 33) {
89+
console.log(`Carr 33 is a ${car.car_year} ${car.car_make} ${car.car_model}`)
90+
}
91+
})
92+
8093
// ==== Challenge 2 ====
8194
// 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.
82-
let lastCar = 0;
83-
console.log();
95+
let lastCar = inventory[inventory.length - 1];
96+
console.log(`The last car is a ${lastCar.car_make} ${lastCar.car_model}`);
8497

8598
// ==== Challenge 3 ====
8699
// 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
87100
let carModels = [];
88-
let carModelsSorted = [];
89-
console.log();
101+
function compareFunction(a, b) {
102+
const modelA = a.car_model.toUpperCase();
103+
const modelB = b.car_model.toUpperCase();
104+
let comparison = 0;
105+
if (modelA > modelB) {
106+
comparison = 1
107+
} else if (modelA < modelB) {
108+
comparison = -1
109+
}
110+
return comparison
111+
}
112+
let carModelsSorted = inventory.sort(compareFunction);
113+
console.log(carModelsSorted);
114+
115+
// Stretch Challenge 3
116+
stretchCarModels = inventory.sort((a, b) => {
117+
const modelA = a.car_model.toUpperCase();
118+
const modelB = b.car_model.toUpperCase();
119+
let comparison = 0;
120+
if (modelA > modelB) {
121+
comparison = 1
122+
} else if (modelA < modelB) {
123+
comparison = -1
124+
}
125+
return comparison
126+
})
127+
console.log(stretchCarModels)
90128

91129
// ==== Challenge 4 ====
92130
// 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.
93131
let carYears = [];
94-
console.log();
132+
for (let i = 0; i < inventory.length; i++) {
133+
carYears.push(inventory[i].car_year)
134+
}
135+
console.log(carYears);
136+
137+
// Stretch challenge 4
138+
let stretchCarYears = inventory.map(car => car.car_year)
139+
// inventory.forEach(car => {
140+
// stretchCarYears.push(car.car_year)
141+
// })
142+
console.log(stretchCarYears)
95143

96144
// ==== Challenge 5 ====
97145
// 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.
98146
let oldCars = [];
99-
console.log();
147+
for (let i = 0; i < carYears.length; i++) {
148+
if (carYears[i] > 2000) {
149+
oldCars.push(carYears[i])
150+
}
151+
}
152+
console.log(oldCars.length);
153+
154+
// Stretch challenge 5
155+
let stretchOldCars = stretchCarYears.filter(year => year > 2000).length
156+
console.log(stretchOldCars)
157+
100158

101159
// ==== Challenge 6 ====
102160
// 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.
103161
let BMWAndAudi = [];
104-
console.log();
162+
for (let i = 0; i < inventory.length; i++) {
163+
if (inventory[i].car_make === "BMW" || inventory[i].car_make === "Audi") {
164+
BMWAndAudi.push(inventory[i])
165+
}
166+
}
167+
console.log(JSON.stringify(BMWAndAudi));
168+
169+
console.log("----------------------")

assignments/function-conversion.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,32 @@
44
// console.log("Function was invoked!");
55
// };
66
// myFunction();
7+
let myFunction = () => {
8+
console.log("Function was invoked")
9+
}
10+
myFunction()
11+
712

813
// let anotherFunction = function (param) {
914
// return param;
1015
// };
1116
// anotherFunction("Example");
17+
let anotherFunction = param => param
18+
anotherFunction("Example")
1219

1320
// let add = function (param1, param2) {
1421
// return param1 + param2;
1522
// };
1623
// add(1,2);
24+
let add = (param1, param2) => param1 + param2
25+
add(1, 2)
1726

1827
// let subtract = function (param1, param2) {
1928
// return param1 - param2;
2029
// };
2130
// subtract(1,2);
31+
let subtract = (param1, param2) => param1 - param2
32+
subtract(1, 2)
2233

2334

2435
// Stretch
@@ -27,4 +38,7 @@
2738
// const triple = exampleArray.map(function (num) {
2839
// return num * 3;
2940
// });
30-
// console.log(triple);
41+
// console.log(triple);
42+
exampleArray = [1, 2, 3, 4]
43+
const triple = exampleArray.map(number => number *3)
44+
console.log(triple)

assignments/objects.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,67 @@ const example = {
1818
}
1919

2020
// Write your intern objects here:
21+
const mitzi = {
22+
id: 1,
23+
name: "Mitzi",
24+
25+
gender: "F",
26+
}
27+
28+
const kennan = {
29+
id: 2,
30+
name: "Kennan",
31+
32+
gender: "M",
33+
}
2134

35+
const keven = {
36+
id: 3,
37+
name: "Keven",
38+
39+
gender: "M",
40+
}
41+
42+
const gannie = {
43+
id: 4,
44+
name: "Gannie",
45+
46+
gender: "M",
47+
}
48+
49+
const antonietta = {
50+
id: 5,
51+
name: "Antonietta",
52+
53+
gender: "M",
54+
}
2255

2356
// ==== Challenge 2: Reading Object Data ====
2457
// Once your objects are created, log out the following requests from HR into the console:
2558

2659
// Mitzi's name
27-
60+
console.log(mitzi.name)
2861
// Kennan's ID
29-
62+
console.log(kennan.name)
3063
// Keven's email
31-
64+
console.log(keven.name)
3265
// Gannie's name
33-
66+
console.log(gannie.name)
3467
// Antonietta's Gender
68+
console.log(antonietta.name)
3569

3670
// ==== Challenge 3: Object Methods ====
3771
// Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided as a hint.
38-
// console.log(kennan.speak());
72+
kennan.speak = function () {
73+
return `Hello, my name is ${this.name}!`
74+
}
75+
console.log(kennan.speak());
3976

4077
// 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));
78+
antonietta.multiplyNums = function (num1, num2) {
79+
return num1 * num2
80+
}
81+
console.log(antonietta.multiplyNums(3,4));
4282

4383
// === Great work! === Head over to the the arrays.js. You may come back and attempt the Stretch Challenge once you have completed the challenges in arrays.js and function-conversion.js.
4484

@@ -49,16 +89,37 @@ const example = {
4989
// 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
5090
// 4. Give each of the objects the ability to speak their names using the this keyword.
5191

52-
const parent = {}
92+
const parent = {
93+
name: "Susan",
94+
age: 70,
95+
speak: function() {
96+
return `Hello, my name is ${this.name}!`
97+
},
98+
child: {
99+
name: "George",
100+
age: 50,
101+
speak: function() {
102+
return `Hello, my name is ${this.name}!`
103+
},
104+
grandchild: {
105+
name: "Sam",
106+
age: 30,
107+
speak: function() {
108+
return `Hello, my name is ${this.name}!`
109+
}
110+
}
111+
}
112+
}
53113

54114
// Log the parent object's name
55-
115+
console.log(parent.name)
56116
// Log the child's age
57-
117+
console.log(parent.child.age)
58118
// Log the name and age of the grandchild
59-
119+
console.log(parent.child.grandchild.name, parent.child.grandchild.age)
60120
// Have the parent speak
61-
121+
console.log(parent.speak())
62122
// Have the child speak
63-
123+
console.log(parent.child.speak())
64124
// Have the grandchild speak
125+
console.log(parent.child.grandchild.speak())

0 commit comments

Comments
 (0)