Skip to content

Commit d34fcc1

Browse files
committed
Add arrays assignment
1 parent 09d082e commit d34fcc1

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

assignments/arrays.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ 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+
let id33 = inventory.find(function(car) {
67+
return car.id === 33;
68+
});
69+
70+
console.log("Car 33 is a", id33.car_year, id33.car_make, id33.car_model);
6771

6872
// ==== Challenge 2 ====
6973
// 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.
70-
let lastCar = 0;
71-
console.log();
74+
let lastCar = inventory[inventory.length - 1];
75+
console.log("Last car in inventory:", lastCar.car_make, lastCar.car_model);
7276

7377
// ==== Challenge 3 ====
7478
// 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
@@ -78,17 +82,30 @@ console.log();
7882
// ==== Challenge 4 ====
7983
// 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.
8084
let carYears = [];
81-
console.log();
85+
for (let i = 0; i < inventory.length; i++) {
86+
carYears.push(inventory[i].car_year);
87+
}
88+
console.log("List of car years in inventory:", carYears);
8289

8390
// ==== Challenge 5 ====
8491
// 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.
8592
let oldCars = [];
86-
console.log();
93+
for (let i = 0; i < carYears.length; i++) {
94+
if (carYears[i] < 2000) {
95+
oldCars.push(carYears[i]);
96+
}
97+
}
98+
console.log("Number of cars in inventory older than 2000:", oldCars.length);
8799

88100
// ==== Challenge 6 ====
89101
// 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.
90102
let BMWAndAudi = [];
91-
console.log();
103+
for (let i = 0; i < inventory.length; i++) {
104+
if (inventory[i].car_make === "BMW" || inventory[i].car_make === "Audi") {
105+
BMWAndAudi.push(inventory[i]);
106+
}
107+
}
108+
console.log("Information for all BMW and Audi cars in inventory:", JSON.stringify(BMWAndAudi));
92109

93110

94111

0 commit comments

Comments
 (0)