Repository navigation
5-6-19 Javascript Project #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| // To help us use arrays with real world problems we are going to simulate a used car dealer that has 50 cars in their inventory. | ||
| // To help us use arrays with real world problems we are going to simulate a used | ||
| //car dealer that has 50 cars in their inventory. | ||
|
|
||
| // The car dealer has all of their inventory housed in the array seen below. Scroll down past the data to find out how you can help the car dealer. | ||
| // The car dealer has all of their inventory housed in the array seen below. | ||
| //Scroll down past the data to find out how you can help the car dealer. | ||
|
|
||
| let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year":2009}, | ||
| let inventory = [ | ||
| {"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year":2009}, | ||
| {"id":2,"car_make":"Mazda","car_model":"Miata MX-5","car_year":2001}, | ||
| {"id":3,"car_make":"Land Rover","car_model":"Defender Ice Edition","car_year":2010}, | ||
| {"id":4,"car_make":"Honda","car_model":"Accord","car_year":1983}, | ||
|
|
@@ -51,7 +54,8 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year" | |
| {"id":47,"car_make":"Volkswagen","car_model":"Jetta","car_year":2007}, | ||
| {"id":48,"car_make":"Dodge","car_model":"Magnum","car_year":2008}, | ||
| {"id":49,"car_make":"Chrysler","car_model":"Sebring","car_year":1996}, | ||
| {"id":50,"car_make":"Lincoln","car_model":"Town Car","car_year":1999}]; | ||
| {"id":50,"car_make":"Lincoln","car_model":"Town Car","car_year":1999} | ||
| ]; | ||
|
|
||
|
|
||
| // Example for loop: | ||
|
|
@@ -62,33 +66,79 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year" | |
| // } | ||
|
|
||
| // ==== Challenge 1 ==== | ||
| // 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: | ||
| console.log(`Car 33 is a *car year goes here* *car make goes here* *car model goes here*` ); | ||
| // 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: | ||
|
|
||
|
|
||
| console.log("Car 33 is a " + inventory[32].car_model + " made by " + inventory[32].car_make + " and the year is " + inventory[32].car_year); | ||
|
|
||
|
|
||
| //console.log("Car 33 is a" inventory[32] "*car year goes here* *car make goes here* *car model goes here*" ); | ||
|
|
||
| // ==== Challenge 2 ==== | ||
| // 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. | ||
| let lastCar = 0; | ||
| console.log(); | ||
| // 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. | ||
|
|
||
| let lastCar = inventory.length - 1; | ||
| console.log(inventory[lastCar]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of |
||
|
|
||
| // ==== Challenge 3 ==== | ||
| // 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 | ||
| let carModels = []; | ||
| console.log(); | ||
| // 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 | ||
|
|
||
| let newArray = [] | ||
| for (let i = 0; i < inventory.length; i++) { | ||
|
|
||
| newArray.push(inventory[i].car_model) | ||
| } | ||
|
|
||
| console.log(newArray.sort()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job breaking this into 2 pieces and just adding the sort to the |
||
|
|
||
|
|
||
| // ==== Challenge 4 ==== | ||
| // 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. | ||
| // 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. | ||
|
|
||
| let carYears = []; | ||
| console.log(); | ||
|
|
||
| for (let i = 0; i < inventory.length; i++) { | ||
| carYears.push(inventory[i].car_year) | ||
| } | ||
|
|
||
| console.log(carYears); | ||
|
|
||
|
|
||
| // ==== Challenge 5 ==== | ||
| // 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. | ||
| // 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. | ||
|
|
||
| let oldCars = []; | ||
| console.log(); | ||
| console.log(carYears[5]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this? |
||
|
|
||
| for (let i = 0; i < carYears.length; i++) { | ||
| if (carYears[i] < 2000) { | ||
| oldCars.push(carYears[i]) | ||
| } | ||
| } | ||
|
|
||
| console.log(oldCars.length); | ||
|
|
||
| // ==== Challenge 6 ==== | ||
| // 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. | ||
| // 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. | ||
|
|
||
| let BMWAndAudi = []; | ||
| console.log(); | ||
|
|
||
| for (let i = 0; i < inventory.length; i++) | ||
|
|
||
| if (inventory[i].car_make === "BMW" || inventory[i].car_make === "Audi") | ||
|
|
||
| BMWAndAudi.push(inventory[i]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of the Don't forget to open and close |
||
|
|
||
| console.log (BMWAndAudi.length) | ||
| console.log(BMWAndAudi); | ||
|
|
||
| let myJSON = JSON.stringify(BMWAndAudi); | ||
|
|
||
| console.log(myJSON); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,26 +5,56 @@ | |
| // }; | ||
| // myFunction(); | ||
|
|
||
| let myFunction = () => { | ||
| console.log("Function was invoked!"); | ||
| } | ||
| myFunction(); | ||
|
|
||
| // let anotherFunction = function (param) { | ||
| // return param; | ||
| // }; | ||
| // anotherFunction("Example"); | ||
|
|
||
| let anotherFunction = (param) => { | ||
| return param; | ||
| } | ||
|
|
||
| console.log(anotherFunction("example")); | ||
|
|
||
| // let add = function (param1, param2) { | ||
| // return param1 + param2; | ||
| // }; | ||
| // add(1,2); | ||
|
|
||
| let add = (param1, param2) => { | ||
| return param1 + param2; | ||
| } | ||
|
|
||
| console.log(add(1,2)); | ||
|
|
||
| // let subtract = function (param1, param2) { | ||
| // return param1 - param2; | ||
| // }; | ||
| // subtract(1,2); | ||
|
|
||
| let subtract = (param1, param2) => { | ||
| return param1 - param2; | ||
| } | ||
|
|
||
| console.log(subtract(1,2)); | ||
|
|
||
|
|
||
| // Stretch | ||
|
|
||
| // exampleArray = [1,2,3,4]; | ||
| // const triple = exampleArray.map(function (num) { | ||
| exampleArray = [1,2,3,4]; | ||
| // const triple = exampleArray.map(function (num) { num * 3 } ) | ||
|
|
||
| // return num * 3; | ||
|
|
||
| // }); | ||
| // console.log(triple); | ||
|
|
||
| // console.log(triple); | ||
|
|
||
| let triple = exampleArray.map((num) => {return num * 3;}); | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job solving this one with
|
||
| console.log(triple); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard coding which car and position in the array you are pointing at and it is a no-no. What if the position of that care shifted because another car has been sold?
What you want to do is iterate through each item in the array testing the
idkey on each object as you do, IF theidmatches then you want to return that data.