Thanks for the detailed explanation Syl - that definitely helps, and I will look at the Stack Overflow link - thanks!
Thanks RamĂłn, I will try going through this today and see if it starts to make any more sense to me!
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!
Hi RamĂłn, or anyone who can help!
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
Hi Jane,
I must have solved this problem quite differently 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?
Good catch on the capital I
!
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!
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.
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
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?
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
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.