Skip to content

Commit 1e497ea

Browse files
committed
arrays challenge completed
1 parent 54d28ed commit 1e497ea

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

assignments/arrays.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,60 @@ 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+
8086
// ==== Challenge 2 ====
8187
// 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();
88+
let lastCar = inventory[inventory.length - 1];
89+
console.log(`The last car is a ${lastCar.car_make} ${lastCar.car_model}`);
8490

8591
// ==== Challenge 3 ====
8692
// 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
8793
let carModels = [];
88-
let carModelsSorted = [];
89-
console.log();
94+
function compareFunction(a, b) {
95+
const modelA = a.car_model.toUpperCase();
96+
const modelB = b.car_model.toUpperCase();
97+
let comparison = 0;
98+
if (modelA > modelB) {
99+
comparison = 1
100+
} else if (modelA < modelB) {
101+
comparison = -1
102+
}
103+
return comparison
104+
}
105+
let carModelsSorted = inventory.sort(compareFunction);
106+
console.log(carModelsSorted);
90107

91108
// ==== Challenge 4 ====
92109
// 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.
93110
let carYears = [];
94-
console.log();
111+
for (let i = 0; i < inventory.length; i++) {
112+
carYears.push(inventory[i].car_year)
113+
}
114+
console.log(carYears);
115+
116+
console.log("======================")
95117

96118
// ==== Challenge 5 ====
97119
// 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.
98120
let oldCars = [];
99-
console.log();
121+
for (let i = 0; i < carYears.length; i++) {
122+
if (carYears[i] > 2000) {
123+
oldCars.push(carYears[i])
124+
}
125+
}
126+
console.log(oldCars);
100127

101128
// ==== Challenge 6 ====
102129
// 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.
103130
let BMWAndAudi = [];
104-
console.log();
131+
for (let i = 0; i < inventory.length; i++) {
132+
if (inventory[i].car_make === "BMW" || inventory[i].car_make === "Audi") {
133+
BMWAndAudi.push(inventory[i])
134+
}
135+
}
136+
console.log(JSON.stringify(BMWAndAudi));

0 commit comments

Comments
 (0)