Hello!
I’m rather bashing my head against the wall with this challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup
My code is as follows:
for (var 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 {return "No such property";}}
It seems to return the last three of six challenges but it doesn’t with the first three. Any ideas as to why this isn’t working?
Kind regards,
B
So your code, as you’ve written it, says:
- start a loop and,
- on the first iteration of the loop, check to see if this contact’s first name matches AND it has the matching property.
- If yes, return that property.
- If no, return “No such property”.
- if that first contact’s name doesn’t match, return “No such contact”.
See something wrong there? At no point does the loop continue to the second, or third or… The early return bug has struck again.
Instead, think about the logic:
- loop through all contacts.
- does the current contact name match?
- If yes, does the current contact have a matching property (SEPARATE CHECK from the name!)
** If yes, return that property (early return works fine here, as you found a match).
** If no, return “No such property” (again, you found a matching name, so early return is fine).
- Here the name doesn’t match. Continue the loop!
- If we’ve checked ALL the names, and fallen out the bottom of the loop, seems we have “No such contact.”
Ah I think I see what you’re saying, albeit I must admit I don’t think I would’ve got there myself which is slightly discouraging.
Essentially if the criteria in the following statement is fulfilled it will pass the return statement and end the loop?
{if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop))
However if one separates out the statement into two if
statements, this allows for the loop to continue running, and thus cycle through the iterations and pass the test?
I must say this element confuses me, as in my mind the [i]
used in the for
loop should continue the cycle regardless of the return statement, as that return statement is nested within an if
condition, and if not met it would simply cycle through the other options and then the length of [i]
would increase and that particular part of the contacts
would be assessed?
Obviously my thoughts are wrong and I thank you for showing the right way through it, although I must admit I’m struggling to make sense of the logic.
Thanks for the response though!
the return statement is 'from the entire function, return this value and end the function." It happens all the time, you’re not the first to be bit by it, and you won’t be the last.
No worries, keep learning!
Ah, having slept on it I think I understand!
So the return function “no such contact” is the default return function, but the nested if/else statements allow for circumstances where other statements are returned but the for loop continues to iterate?
Think I’m almost there, will spend some time logging a manual output of the test to see if I can make this more intuitive to me.
Many thanks!
1 Like