Profile Lookup [Spoilers] [Solved]

A return statement returns from a function, therefore once you hit the return 'No such contant', you shold be sure that there is no more elements to be checked, that means once the for llop is over :wink:

Thanks boss @yhabib. I just posted a snip of my page and the responses I’ve bn getting. I’ve checked everything but Still haven’t been able to figure out what I’m not doing right. Thanks.

The problem with your code is that in the first iteration of the contacts array you are going to return a value, so you are not checking the rest of the array.

Suppose contacts is: var contacts = [ {"firstName": "Akira"}, { firstName": "Harry"}] and the value to check is Harry your function wil return No such contact. Because after the first iteration, Akira will reach a return therefore the function will return this value.

The solution it would be to return the No such contact once your for loop is over.

Pseudocode:
for(contact in contacts) if(firstName === contact.firstName && hasProperty) return contant; else return "No property" endfor return "No user"

Thanks @yhabib. I’m still racking my brain around it. I’ll let you know, when I find my way around it.
Thanks once again.

No prob at all. Maybe I didn’t explain it very well, I ahave to improve my communication skills. Ask anything!

Glad to help!

Hey boss @yhabib! Finally came up with a working code but I need you or someone else to explain why I had to put the last “return” statement outside my “for” loop. Please, check code below:

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

return “No such contact”;
// Only change code above this line
}

@ruudnistelrooy I am glad that you made it.
Another example, hope this is one helps you :wink:

var a = ['Peter', 'Kobe', "Kevin']; it is a array of names
var query = 'Kevin'; what we want to find in the array

function checkArray(a, query) { for(var i=0; i<a.length; i++) { if(a[i] === query) return "Found it!!!" else return "It does not exist in the array!" } }

So what’s going on?

  • With the first iteration your if else conition, checks if Kevin === Peter but is false therefore your else statemente is triggered returning “It does not exist in the array” because a return statement exits a function no matter how many more values are in the array to be inspectionated.

  • Putting the else return outside the for loop, you are ensuring that the comparison is being made before returning any value from the function

function lookUpProfile(firstName, prop){

  for(var k = 0; k < contacts.length; k++) {
      if(contacts[k]["firstName"] == firstName) {
        if(contacts[k].hasOwnProperty(prop)){
          return contacts[k][prop];
          //if he does have the first but not the second, it's because he doesn't have such a property
        }else{
          return ("No such property");
        }
     }
  }
  //We get out
  return ("No such contact");
}

But at first, i i tried some thing different it didn’t work and i’m not sure why:

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

// Change these values to test your function
lookUpProfile("Kristian", "lastName");

Thanks for the explanation @yhabib. I think I now understand it better. I obviously need to study even harder. You’ve been very helpful. Thanks, once again.

Your answer works but I am not sure why? What did you create the empty array names for?

but it works!

}

I went nuts trying to find a solution for this exercise. I couldn’t understand how my (apparently) correct code didn’t work.
Turns out I had written lenght instead of length :sweat:

2 Likes

can’t get to work because of the error, that’s why i used try and catch
and realize i don’t have to. but it works

ah! at last i think i got this one. it took me a long time to understand what the “for loop” was doing, as i was not getting “No such contact” part right. So, here is my code:


function lookUpProfile(firstName, prop){
// Only change code below this line

for (var i = 0; i < contacts.length; i++) {
if (firstName === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].hasOwnProperty(prop) !== true) {
return “No such property”;
}
}
return “No such contact”;

// Only change code above this line
}

2 Likes

This worked, plus I actually understand what you did. Thank You

MK-61 Thank you very much for the help!!! I had the same logic but I putted that last else in wrong place!

For this challenge, I was going with the assumption we are supposed to only use the knowledge acquired from the previous challenges. Just my personal opinion.

Here is my solution

function lookUpProfile(firstName, prop) {
    // Only change code below this line
    for (var i = 0; i < contacts.length; i++) {
        if (!contacts[i].hasOwnProperty(prop)) {
            return "No such property";
        }

        if (contacts[i].firstName === firstName) {
            return contacts[i][prop];
        }
    }

    return "No such contact";
    // Only change code above this line
}

Gosh! Thanks, man, I had the same problem and I simply couldn’t see that my return statement is inside the loop.

This is a beautiful solution. thx

Hi Nims,
I’m glad I’m not the only one getting stuck with things like that.
I had the same exact issue. Returning the result before finishing running my for loop through the entire array.