Skip to content

sachin benny js1 - #1

Open
sachben91 wants to merge 1 commit into
masterfrom
sachin-benny
Open

sachben91 wants to merge 1 commit into
masterfrom
sachin-benny

Conversation

@sachben91

Copy link
Copy Markdown
Owner

@Mister-Corn Mister-Corn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 inventory to 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!

Comment thread assignments/arrays.js
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

Comment thread assignments/arrays.js
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.

// 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

Comment thread assignments/objects.js
"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.

Comment thread assignments/objects.js
// ==== 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants