Profile Lookup, code not running

Tell us what’s happening:

Your code so far


//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(name, prop){
// Only change code below this line
for( var j = 0; j<contacts.length; j++){
    if(contacts[j].firstname === name){
        if(contacts[j].hasOwnProperty(prop)){
            return contacts[j][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");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

So, when you run that, there’s a message that pops up in the lower right pane. That’s the FreeCodeCamp “console”, a limited version of the developer’s console in the browser, and it’s telling you there’s a problem. Here’s what it says:

SyntaxError: unknown: Unexpected token (40:5)
  38 |             }
  39 |         }
> 40 |     }else{ 
     |      ^
  41 | return "No such contact";
  42 |    }
  43 | }

So it doesn’t like that else going on there. In order to find out why, take a look at how your { and } are nesting. One of them before the else needs to be moved somewhere else.

so what exactly is supposed to be done

If you look at the OP’s code, you kind of have to do a little detective work. Look at the open brackets {, and make sure there’s a matching close }. In the OP’s case, he closed once more than he opened, thus the else statement was being seen by the javascript engine as being stuck on the wront place. Here’s what teh JS engine was seeing:

for(...){
  if(...){
    if(...){
      // inner if clause
    } else {
      // inner if's 'else' clause
    } // end of inner if statement
  } // end of outer if statement
} else { 
  // this is on the for(...) loop, what do I do with THAT?!
  return "No such contact";
} // end of for(...) loop's else clause, which caused the problem

So, if you want an else statement, it has to be mated to an if statement. In the OP’s case, it was not.

In the event that the loop had run through all records and not returned anything (the early return hadn’t been used), then no matching records were found. So that would mean that we fell out the bottom of the for loop, and we simply want a return following the for(...) loop. Just put the return "No such contact"; statement following the for loop, no else or other statement needed.