Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions assignments/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);}
}

Copy link
Copy Markdown

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:

const arrOf4Nums = [0, 1, 2, 3]; // notice that 3 is the last, which corresponds to index
const arrOf10Nums = [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9]; // notice 9 is last

That means you could generalize the above to

inventory[inventory.length - 1];

That would get you the last element.

Copy link
Copy Markdown
Owner Author

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


// ==== 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]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console logging carModels would have worked just as well here too.

}

// ==== 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));
28 changes: 11 additions & 17 deletions assignments/function-conversion.js
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}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember there's a difference between console.log and return. console.log allows you to print some output to some external display, the console. You can see that console in the browser console or in a terminal in node for example. console.log does not determine what the output of a function is.

In contrast, return signals the end of the function's execution, and any expression after it is designated to be the output of the function.

Why is this important? Well, looking at your function, you console.log the output instead of returning. This is OK in isolation:

const add = (param1, param2) => console.log(`${param1 + param2}`);
add(1,2); // 3 is logged to the console

But 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 console

Because 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.5

Unless the exercise specifically tells you to, assume the output is to be returned, and console.log it afterwards.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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
Expand All @@ -27,4 +21,4 @@
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);
// console.log(triple);
4 changes: 2 additions & 2 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

<title>JS I</title>

<script src="objects.js"></script>
<script src="objects.js"></script>
<script src="arrays.js"></script>
<script src="function-conversion.js"></script>
</head>

<body>
<h1>Check your work in the console!</h1>
</body>
</html>
</html>
56 changes: 46 additions & 10 deletions assignments/objects.js
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
Expand All @@ -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}`)};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging intern2's name is a little brittle. Meaning, what about if the variable name was different? What about if the object is inside an array of intern objects? In those situations, the name in the object is still "Kennan", but the speak method won't work because it depends on the "Kennan" object to be called intern2.

You could just hardcode Kennan's name in the console.log to get around this. You can also use the this keyword, though you cannot use the arrow function to do that:

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 undefined

We'll learn more about the this keyword in future lessons.

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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This console.log returns undefined. This is because in order to get a value out of a function, the function needs to return it. Think how you can change your speak method so that it can return the string that can be console.log'ed here (rather than have the method console.log directly).


// 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.
Expand Down