Repository navigation
Conversation
Mister-Corn
left a comment
There was a problem hiding this comment.
Assignment Objectives
Objects
- Complete each challenge presented before moving on to Arrays.
Arrays
Use for loops and arrays to solve their problems.
- Utilize the the array
inventoryto complete your challenges - You are not permitted to use map, reduce, or filter to solve these problems. Only use a basic for loop.
- Complete each challenge presented before moving on to stretch.
Arrow Function Syntax
- Convert functions to Arrow Function Syntax
Comments
Good work on the project, Sachin! 👍 You're doing a good job trying out things, learning the concepts, and building proficiency in JS. I added very chunky comments, so please take a look when you have time.
Again, nice job!
| lastCar = inventory.length; | ||
| if (inventory[i].id === lastCar) | ||
| {console.log(`The last car is ${inventory[i].car_make} ${inventory[i].car_model}`);} | ||
| } |
There was a problem hiding this comment.
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 lastThat means you could generalize the above to
inventory[inventory.length - 1];That would get you the last element.
There was a problem hiding this comment.
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
| carModels.sort(); | ||
| for (let i = 0; i<carModels.length; i++) | ||
| { | ||
| console.log(carModels[i]); |
There was a problem hiding this comment.
Console logging carModels would have worked just as well here too.
| // return param1 + param2; | ||
| // }; | ||
| // add(1,2); | ||
| const add = (param1, param2) => console.log(`${param1 + param2}`); |
There was a problem hiding this comment.
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 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 console.log it afterwards.
There was a problem hiding this comment.
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
| "gender":"M" | ||
| } | ||
| intern2.speak = () => { | ||
| console.log(`Hello , my name is ${intern2.name}`)}; |
There was a problem hiding this comment.
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 undefinedWe'll learn more about the this keyword in future lessons.
| // ==== 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.
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).
@Mister-Corn