Repository navigation
sachin benny js1 #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 |
|---|---|---|
|
|
@@ -60,35 +60,69 @@ let inventory = [{"id":1,"car_make":"Lincoln","car_model":"Navigator","car_year" | |
| // for (let i = 0; i < arr.length; i++) { | ||
| // arr[i]; // 1,2,3,4 | ||
| // } | ||
|
|
||
| console.log('hello'); | ||
| // ==== 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*` ); | ||
| for (let i = 0; i< inventory.length; i++){ | ||
| if (inventory[i].id === 33) | ||
| { | ||
| console.log(`The car is ${inventory[i].car_year} ${inventory[i].car_make} ${inventory[i].car_model}`) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // ==== 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(); | ||
| for (let i=0; i< inventory.length; i++) | ||
| { | ||
| lastCar = inventory.length; | ||
| if (inventory[i].id === lastCar) | ||
| {console.log(`The last car is ${inventory[i].car_make} ${inventory[i].car_model}`);} | ||
| } | ||
|
|
||
| // ==== 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(); | ||
| for (let i=0; i<inventory.length; i++) | ||
| { | ||
| carModels.push(inventory[i].car_model); | ||
| } | ||
| carModels.sort(); | ||
| for (let i = 0; i<carModels.length; i++) | ||
| { | ||
| console.log(carModels[i]); | ||
|
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. Console logging |
||
| } | ||
|
|
||
| // ==== 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. | ||
| let carYears = []; | ||
| console.log(); | ||
| for (let i=0; i<inventory.length; i++) | ||
| { | ||
| carYears.push(inventory[i].car_year) | ||
| console.log(carYears[i]) | ||
| } | ||
|
|
||
|
|
||
| // ==== 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. | ||
| let oldCars = []; | ||
| console.log(); | ||
|
|
||
| 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. | ||
| let BMWAndAudi = []; | ||
| console.log(); | ||
|
|
||
|
|
||
|
|
||
| for (let i=0; i<inventory.length; i++) | ||
| { | ||
| if(inventory[i].car_make === "Audi" || inventory[i].car_make === "BMW") | ||
| { | ||
| BMWAndAudi.push(inventory[i]) | ||
| } | ||
| } | ||
| console.log(JSON.stringify(BMWAndAudi)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,18 @@ | ||
| // Take the commented ES5 syntax and convert it to ES6 arrow Syntax | ||
|
|
||
| // let myFunction = function () { | ||
| // console.log("Function was invoked!"); | ||
| // }; | ||
| // myFunction(); | ||
| const myFunction = () => { | ||
| console.log("Function was invoked!"); | ||
| }; | ||
| myFunction(); | ||
|
|
||
| // let anotherFunction = function (param) { | ||
| // return param; | ||
| // }; | ||
| // anotherFunction("Example"); | ||
| const anotherFunction = param => console.log(`${param}`); | ||
| anotherFunction("Example"); | ||
|
|
||
| // let add = function (param1, param2) { | ||
| // return param1 + param2; | ||
| // }; | ||
| // add(1,2); | ||
| const add = (param1, param2) => console.log(`${param1 + param2}`); | ||
|
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. Remember there's a difference between In contrast, Why is this important? Well, looking at your function, you const add = (param1, param2) => console.log(`${param1 + param2}`);
add(1,2); // 3 is logged to the consoleBut what about if I need to use the output of add? const add = (param1, param2) => console.log(`${param1 + param2}`);
console.log(add(1,2) / add(3,3)); // this outputs NaN (not a number)
// 3 and 6 is output to the consoleBecause the function doesn't return anything, I'm unable to use the output of add for other operations. That severely limits the usefulness of the function. Instead, have the functions return their value, so you can use those values! const add = (param1, param2) => param1 + param2;
console.log(add(1,2)); // console.logs 3
console.log(add(1,2) / add(3,3)); // console.logs 0.5Unless the exercise specifically tells you to, assume the output is to be returned, and
Owner
Author
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. ah got it, this was a quick work around to get an output that I can see in the console. But I see your point, will edit this |
||
| add(1,2); | ||
|
|
||
| // let subtract = function (param1, param2) { | ||
| // return param1 - param2; | ||
| // }; | ||
| // subtract(1,2); | ||
| const subtract = (param1, param2) => console.log(`${param1 - param2}`); | ||
| subtract(1,2); | ||
|
|
||
|
|
||
| // Stretch | ||
|
|
@@ -27,4 +21,4 @@ | |
| // const triple = exampleArray.map(function (num) { | ||
| // return num * 3; | ||
| // }); | ||
| // console.log(triple); | ||
| // console.log(triple); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // Let's get some practice writing a few objects for a new group of interns at a small business. | ||
|
|
||
| // ==== Challenge 1: Writing Objects ==== | ||
| // ==== Challenge 1: Writing Objects ==== | ||
| // HR needs some information on the new interns put into a database. Given an id, email, first name, and gender. Create an object for each person in the company list: | ||
|
|
||
| // 1,mmelloy0@psu.edu,Mitzi,F | ||
|
|
@@ -18,31 +18,67 @@ const example = { | |
| } | ||
|
|
||
| // Write your intern objects here: | ||
| const intern1 = { | ||
| "id": 1, | ||
| "email":"mmelloy0@psu.edu", | ||
| "name": "Mitzi", | ||
| "gender":"F" | ||
| } | ||
| const intern2 = { | ||
| "id": 2, | ||
| "email":"kdiben1@tinypic.com", | ||
| "name":"Kennan", | ||
| "gender":"M" | ||
| } | ||
| intern2.speak = () => { | ||
| console.log(`Hello , my name is ${intern2.name}`)}; | ||
|
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. Logging You could just hardcode Kennan's name in the intern2.speak = function() {
console.log(`Hello , my name is ${this.name}`)}; // Hello , my name is Kennan❗️ Arrow Function: intern2.speak = () => {
console.log(`Hello , my name is ${this.name}`)}; // Hello , my name is undefinedWe'll learn more about the |
||
| const intern3 = { | ||
| "id": 3, | ||
| "email":"kmummery2@wikimedia.org", | ||
| "name":"Keven", | ||
| "gender":"M" | ||
| } | ||
| const intern4 = { | ||
| "id":4, | ||
| "email":"gmartinson3@illinois.edu", | ||
| "name":"Gannie", | ||
| "gender":"F" | ||
| } | ||
| const intern5 = { | ||
| "id":5, | ||
| "email":"adaine5@samsung.com", | ||
| "name":"Antonietta", | ||
| "gender":"F" | ||
| } | ||
| intern5.multiplyNums = (num1,num2) => { | ||
| return num1*num2;} | ||
|
|
||
| const interns = [intern1, intern2, intern3, intern4, intern5]; | ||
|
|
||
| // ==== Challenge 2: Reading Object Data ==== | ||
| // ==== Challenge 2: Reading Object Data ==== | ||
| // Once your objects are created, log out the following requests from HR into the console: | ||
|
|
||
| // Mitzi's name | ||
| console.log(`${intern1.name}`); | ||
|
|
||
| // Kennan's ID | ||
|
|
||
| console.log(`${intern2.id}`); | ||
| // Keven's email | ||
|
|
||
| console.log(`${intern3.email}`); | ||
| // Gannie's name | ||
|
|
||
| console.log(`${intern4.name}`); | ||
| // Antonietta's Gender | ||
|
|
||
| // ==== Challenge 3: Object Methods ==== | ||
| console.log(`${intern5.gender}`); | ||
| // ==== Challenge 3: Object Methods ==== | ||
| // Give Kennan the ability to say "Hello, my name is Kennan!" Use the console.log provided as a hint. | ||
| // console.log(kennan.speak()); | ||
| console.log(intern2.speak()); | ||
|
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. This |
||
|
|
||
| // Antonietta loves math, give her the ability to multiply two numbers together and return the product. Use the console.log provided as a hint. | ||
| //console.log(antonietta.multiplyNums(3,4)); | ||
| console.log(intern5.multiplyNums(3,4)); | ||
|
|
||
| // === Great work! === Head over to the the arrays.js file or take a look at the stretch challenge | ||
|
|
||
| // ==== Stretch Challenge: Nested Objects and the this keyword ==== | ||
| // ==== Stretch Challenge: Nested Objects and the this keyword ==== | ||
|
|
||
| // 1. Create a parent object with properties for name and age. Make the name Susan and the age 70. | ||
| // 2. Nest a child object in the parent object with name and age as well. The name will be George and the age will be 50. | ||
|
|
||
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.
I see your logic here, and it works. However, is it truly necessary to go through the entire array to get the last car object in the array?
The challenge is asking you just for the last object in the array. Notice that there are 50 objects in the array. Array indexing starts at 0, so the last object in the array would be at index 49 (the 50th index).
Therefore, you could simply say:
inventory[49]Notice that because of the zero-based indexing, the index of the last element of the array is one less than the index. Try playing with that a bit:
That means you could generalize the above to
That would get you the last element.
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.
Got it.I basically wanted to just make something that would work regardless of the size of the array, which is why I went for it