Going slightly mad

Well, this is not just the Queen song, it’s my current state of mind.
I am working my way through the JavaScript Basics and I am having a hard time near the end.

This is my solution to the “Profile Lookup” problem:

/*****************************************

function lookUpProfile(name, prop){

for ( var i = 0; i < contacts.length; i++) {
  if (name === contacts[i].firstName) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else return "No such property";
  } else return "No such contact";
}

******************************************/

And this is the solution on the FFC website:

/*******************************************

for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === name) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

******************************************/

Theirs is working, mine doesn’t. It passes the last conditions, the ones with No such contact / property but not the first three ones, which input the data to be returned. And I spent an hour already trying to figure it out and it is just so frustrating, I am literally crying.

Thank you.

I don’t know if you noticed, you have to use this return statement after for loop.
Do you know what could be the reason for that?

Yours is not the same, if the name isn’t right on the first iteration you’re returning, which exits the loop and the function.

Mixing if/else with and without brackets is confusing to read, which may not help

1 Like

Yes, that was the issue. It seems so simple once someone else points it. Thank you Dan.

1 Like