Basic JavaScript Day 4: Homework

Thanks for the detailed explanation Syl - that definitely helps, and I will look at the Stack Overflow link - thanks! :slight_smile:

2 Likes

Thanks RamĂłn, I will try going through this today and see if it starts to make any more sense to me! :slight_smile:

2 Likes

Hello Ramon, I find some exercises a bit confusing for me in the homework practices.

Hey there! Do you need help with a particular one?

Happy to take a look at any of your code and seeing what you’ve tried on an exercise.

Remember, here and on Discord you can make forum posts for help on any particular problem you’re stuck on. That’s what we’re here for!

1 Like

Hi RamĂłn, or anyone who can help! :slight_smile:

I have a question about the Profile Lookup exercise, which I now finally understand - I followed the video explanation on freecodecamp and noted a bunch of things down using an old-fashioned pen and paper. My problem with writing these codes is that my original attempts always seem to be extremely long-winded.

OK - so my question is this - in the code

if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop))

why doesn’t

if (contacts[i].firstName === name && contacts[I].hasOwnProperty(prop)) {

work as an if clause? As this is what I was trying, and failing with.

And for the other lines I was using the && operator as well.

Thank you :slight_smile:

Hi Jane,

I must have solved this problem quite differently :slight_smile: as I used early returns

But looking at your code snippet, is this literally taken from your code? as you use contacts[i] (lowercase) in the first part of your if statement and contacts[I] (uppercase) in your second part?

2 Likes

Good catch on the capital I!

2 Likes

Thank you Syl

I did not know this tool, it is awesome!! Python Tutor: Visualize code in Python, JavaScript, C, C++, and Java

Shame I didn’t know before, but still will help me a lot. I think you should post about this tool in a separate post, it will help a lot of people in their learning process. Just a suggestion, not an imposition.

Thank you again, your post are gold, have a great weekend!

2 Likes

Hi Syl! I’m pretty sure the capital [I] wasn’t in my original code - I was pasted together the two lines from the two if lines above to make the && line and was editing in the text box on here. :slight_smile:

Syl/RamĂłn: Is it OK to use the && operator in this statement?

My attempt from earlier:

function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i].prop;
} else if
(contacts[i].firstName !== name) {
return “No such contact”;
} else if
(contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)) {
return “No such property”
}
}

// Only change code above this line
}

When I run

console.log(lookUpProfile(“Sherlock”, “likes”));

, the console outputs

“No such contact”

General comment - I find it very hard to distill all the possibilities down to a couple of lines of code. My usual approach is quite long-winded, as above.

Should I forget about using the && operator here?

Thanks, Jane :slight_smile:

1 Like

Hi Jane,

You’re code formatting above is a bit hard to follow, but I did copied it to my editor to analyze a bit better what is going on. Your trouble comes from the if/else-if checks within the loop that basically interrupt your loop on the first go as you ‘return’ from the function.

Lets have a closer look with your Sherlock contact:

function lookUpProfile(name, prop) {
// Only change code below this line
  for (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i].prop;
    } else if (contacts[i].firstName !== name) {
      return 'No such contact';
    } else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)) {
      return 'No such property'
    }
  }
// Only change code above this line
}
console.log(lookUpProfile('Sherlock', 'likes'));

So you try to loop through all the contacts, and to see if we have ‘Sherlock’ with the prop ‘likes’.
When we step into the code the first time around, you do this check:

if (contacts[0].firstName === name && contacts[0].hasOwnProperty(prop))  { ... }  //  the first contact is 'Akira' thus False

we move on…

else if (contacts[0].firstName !== name) { ... }  //the first contact is 'Akira', which is not 'Sherlock' thus this is True. Yay! We have found our answer and let's return 'No such contact'. Yay! (or Nay!)

It simply stops here, the function returns here an unintended answer. It never even got to ‘Sherlock’ in the list. It did not even do a second loop. It simply thought it was done with the first contact on the first run through the for loop.

You can inspect these kind of iterations really easy with a visualization. I’ve popped in the code so you can see how this goes: Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java

I hope this explanation helps?

I solved it, as mentioned a bit different. I looked first in the array if my contact existed (if not, then done), if found, then does it have this prop (if not, then done) and lastly, if I am still running the code then 1. the contact exists, 2. he has the prop, so return the prop.

hint: there is a ‘find’ method on Array, but you totally can do this with a loop. Remember if you find what you are after, you got to interrupt the loop (break or return), if you did not find what you were after, you can continue.

With a loop my approach would be to just check ‘if you have the contact’ (contact[i].firstName === name) and then tell it to ‘continue’ if that is not the case. If it loops through everything without finding the contact, then it was not there. I do not believe you can solve this with an && (if you look at your code you can see this as a starting condition everywhere, so that indicates a level of abstraction).

I hope this helps?

2 Likes

Hi Syl, this is so helpful thank you. I was not really seeing this as an iterative process at all, rather, I was jumping straight in with the “Sherlock” parameters and seeing whether they fit within in the function, and seeing that the first if clause met the remit of having the name and the property for “Sherlock”, I had thought this meant the function would output the property of that contact. I was not thinking of how it must iterate through each object in turn (therefore starting with “Akira”, not “Sherlock”, before deciding whether to continue running.
I will continue working and reworking this problem until I fully understand it, but I definitely feel there I have finally had a breakthrough. I am going through the visualizer step by step as well, so thanks for that too.
All the best, Jane :smiling_face:

3 Likes

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.