Profile Lookup [Spoilers] [Solved]

One suggestion on your pseudocode - if you’re going to use found as a variable - change ‘is found’ to exists or some other usage that doesn’t use the word found - it’s confusing

Thanks for pointing that out, just fixed it to avoid that issue.

Also

While not deceptively simple - your psuedocode is more complicated than need be.

There’s no need for your ‘found variable’ at all - what purpose does it serve, when you get to return in javascript, the function is over - no longer executed.

The boolean is for catching edge cases like “Bob” that aren’t in any of the objects in the list, but you have to iterate through the entire list in order to make that determination. If a name is found anywhere in the list, found gets set to true, otherwise, if it’s not set to true by the end of the loop and is still false, then it can be concluded that the name wasn’t in the list.

But the edge case isn’t necessary

If you loop through the array and bob doesn’t exist it just ‘falls through’ to return no such contact - it’s how i did it```
Start Loop
if to check for name on each array member
if to check for property if name exists
return property if property exists
else
return no property if property doesn’t exist
end
end
end


The boolean isn't required for the edge case - the edge case will fall through because you have no 'else' instance if the contact doesn't exist...the loop just goes on to the next one until it's done

(I know this works cause it's how I solved it)
1 Like

I misunderstood your earlier question about the boolean variable itself, you’re right that it’s not necessary and the logic in the pseudocode works the same without it. I added it mainly for my own understandability of the logic, could just as easily take that out.

Ah - ok - now that makes sense - reason I asked is that I originally tried it with a variable that would change setting depending on outcome and it failed to work properly.

Sometimes coming from another coding environ is a detriment

Stuck on this as well and shameless bump.

I did the same thing as the OP and built it out as an If / else if statement.

If I understand some of yours posts correctly, in a for loop once it hits an if statement which is true, it stops. If this is the case I highly suggest this fact is included somewhere earlier in the lessons… It isn’t… seems like a major point that was left out.

The loop doesn’t stop but that iteration of the loop stops - because that’s kind of how loops work - if they are true they they do what they’re supposed to do and move on…perhaps a link to your code would be helpful

I guess there is some principle or logic around an some part of this that I do not understand. So if someone could explain it to me I would appreciate it.

If I run each of these if statements by themselves (taking out the other 2 parts) they execute correctly. However when I throw them together, it doesn’t want to work.

Right now the “No such property” & the “no such contact” part work and meet the objectives when I run it, but the first if statement does not. However if I take the else if’s out, and just leave the first if statement, then it runs and meets the requirements.

I understand there is a more efficient way of writing this. I guess I am missing some “rule” or logic as to why this would not also work.

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) === true) {
return contacts[i][prop];
} else if (firstName !== contacts[i].firstName) {
return “No such contact”;
} else if (contacts[i].hasOwnProperty(prop) === false) {
return “No such property”;
}

}
// Only change code above this line
}

Hello there,

i cannot somehow grasp the difference between if/else and for loop, there is a big one obviously, in this challenge.

I intended to complete this one only with if/else. I have scrolled through a couple of threads, the most satisfying answer for me was that if/else declaration cannot be used with arrays consisting of more objects…could You maybe offer me some elemetary explanation ?

Thank You!

It’s helpful to see if/else and for-loops as pictures.

If-else
// ... code before if/else

if (/* condition is true */) {
  // This is the code inside of the if-block
}
else {
  // This is the code inside of the else-block
}

// ... code after if/else

Note that the flow of execution goes straight down. Each part is executed only once.

for-loops
// ... code before for-loop

// the for-loop header's syntax is
// initialization; condition; increment.
for (var i = 0; i < 10; i++) {
  // ... code inside for-loop
}

// ... code after for-loop

See the loop in the flowchart? First the initialization is executed, then the condition is checked. If it is true, the code in the for-loop block is executed. After that the increment is run. Then you go back to checking the condition, repeating the loop until finally the condition is false. At that point you are out of the for-loop

As you can see, you can’t use if/else alone to check each item in the array. For-loops and arrays are a perfect match. Sure in this challenge you can ditch looping and create an if/else block for every element in the array, but in practice you’ll most likely not know in advance how many elements there are going to be in the array you’re working with. So you’ll be using loops.

Typically, the for-loop’s header when working with arrays is

for (var i = 0; i < array.length; i++)

Hi Kev,

I somehow assumed that if/else scans the whole array.

Do I understand it correctly that if using if/else solely I would end up checking only the first object of the array, in this case Akira Lane and would not move any further ?

Dusan

You can’t use just one if/else to scan the whole array. I don’t know of any way to make it possible.

However I imagine that you can set up multiple if/else blocks to check each element. Like, one for the first element, another for the second, and so on. But I won’t go for this approach.

1 Like

I was really proud of myself when I saw this, because I came up that same logic, but for some reason my code is still not working. What am I missing? I put in comments to show what I was trying to do:

for (i=0; i < contacts.length; i++)

//does contact have desired first name?
if (firstName == contacts[i][firstName]) { 
        
     //if yes, does it have desired prop?*/   
    if (contacts[i].hasOwnProperty(prop)) {
    
      //if yes,  give the prop's value   
     return contacts[i][prop];}
  
      //if no, error message;
      else {console.log("No such property");}
  
}
//if not desired name, is this the last entry?
if (i == contacts.length) {

//if yes, error message:
  console.log("No such contact");}

// Only change code above this line
}

D

i have the same problem with you. Why did no one explain this? If you already got the answer, can u explain to me please? thank you :smiley:

Here is my answer:
function lookUpProfile(firstName, prop){
var checkName= false;
var checkProp = false;
for(var i=0; i < contacts.length; i++){
if(contacts[i].firstName === firstName)
checkName = true;
if(contacts[i].hasOwnProperty(prop))
checkProp = true;
if ( checkName && checkProp){
return contacts[i][prop];
}
}
if(!checkName){
return “No such contact”;
}
if(!checkProp){
return “No such property”;
}
}

Hi guys! Please, I need someone to help me go through my code and tell me what I did wrong. I can’t seem to proceed to the next challenge. Thanks.

//Setup
var contacts = [
{
“firstName”: “Akira”,
“lastName”: “Laine”,
“number”: “0543236543”,
“likes”: [“Pizza”, “Coding”, “Brownie Points”]
},
{
“firstName”: “Harry”,
“lastName”: “Potter”,
“number”: “0994372684”,
“likes”: [“Hogwarts”, “Magic”, “Hagrid”]
},
{
“firstName”: “Sherlock”,
“lastName”: “Holmes”,
“number”: “0487345643”,
“likes”: [“Intriguing Cases”, “Violin”]
},
{
“firstName”: “Kristian”,
“lastName”: “Vos”,
“number”: “unknown”,
“likes”: [“Javascript”, “Gaming”, “Foxes”]
}
];

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”;
}
}

else {
    return "No such contact";
  }

}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile(“Akira”, “likes”);

You’re so close! Think about where you should return "No such contact";. in your code.

Thanks for your response @kevcomedia.

My last write-up was all over the place. Perhaps, you should take a look at my function below. I can’t seem to figure out what I’m not doing right.

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”;
}
}

else {
    return "No such contact";
  }

}
// Only change code above this line
}