Skip to content

Commit 0a36ba7

Browse files
author
Jacob Angulo
committed
fiished array.js assignment, moving on to object.js
1 parent af193aa commit 0a36ba7

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

assignments/arrays.js

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,101 @@ 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*` );
6766

67+
function getCarById (id) {
68+
let car;
69+
for (i = 0; i< inventory.length; i++) {
70+
if (inventory[i]["id"] == id) {
71+
car = inventory[i];
72+
}
73+
}
74+
console.log(`Car ${car["id"]} is a ${car["car_year"]} ${car["car_make"]} ${car["car_model"]}` );
75+
}
6876

77+
getCarById(33);
6978

7079
// ==== Challenge 2 ====
7180
// 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();
81+
82+
let lastCar = inventory[(inventory.length - 1)];
83+
console.log(lastCar["car_make"] + lastCar["car_model"]);
7484

7585
// ==== Challenge 3 ====
7686
// 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
7787
let carModels = [];
78-
console.log();
88+
89+
function sortCarModels (arr) {
90+
let models = [];
91+
for (let i = 0; i < arr.length; i++) {
92+
models.push(arr[i]["car_model"])
93+
}
94+
let sorted = models.sort(function(a, b) {
95+
let x = a.toLowerCase();
96+
let y = b.toLowerCase();
97+
if (x < y) {
98+
return -1;
99+
}
100+
if (y < x) {
101+
return 1;
102+
}
103+
return 0;
104+
});
105+
console.log(sorted);
106+
return sorted;
107+
}
108+
109+
sortCarModels(inventory);
79110

80111
// ==== Challenge 4 ====
81112
// 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.
82-
let carYears = [];
83-
console.log();
113+
//let carYears = [];
84114

115+
function getCarYears(arr) {
116+
let carYears = [];
117+
for (let i = 0; i < arr.length; i++) {
118+
carYears.push(arr[i]["car_year"])
119+
}
120+
//console.log(carYears);
121+
console.log(carYears);
122+
return carYears;
123+
}
124+
125+
var dealershipCarYears = getCarYears(inventory);
85126
// ==== Challenge 5 ====
86127
// 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.
87-
let oldCars =[];
88-
console.log();
128+
129+
function olderCars(arr) {
130+
let oldCars =[];
131+
for (let i = 0; i < arr.length; i++) {
132+
if (arr[i] < 2000) {
133+
oldCars.push(arr[i]);
134+
}
135+
}
136+
console.log(oldCars);
137+
return oldCars;
138+
}
139+
140+
olderCars(dealershipCarYears);
141+
89142

90143
// ==== Challenge 6 ====
91144
// 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.
92-
let BMWAndAudi =[];
93-
console.log();
145+
146+
function carSearch (args, arr) {
147+
let inquiry = args.split(' ');
148+
let searched = [];
149+
for (let i = 0; i < inquiry.length; i++) {
150+
for (let j = 0; j < arr.length; j++) {
151+
if (arr[j]["car_make"] == inquiry[i]) {
152+
searched.push(arr[j])
153+
}
154+
}
155+
}
156+
console.log(JSON.stringify(searched))
157+
}
158+
159+
carSearch(("Audi BMW"), inventory);
160+
94161

95162

96163

assignments/objects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const example = {
1717
"gender": "F"
1818
}
1919

20-
// Write your intern objects here:
20+
// Write your intern objects here:
2121

2222

2323
// ==== Challenge 2: Reading Object Data ====

0 commit comments

Comments
 (0)