Profile lookup error for all test case & syntax error

Tell us what’s happening:
I don’t know why the console says there is an error. I’ve tried to debug my code for hours but found no mistake so far. can u help me to find out my mistake

  **Your code so far**

// Setup
var contacts = [
  {
      "firstName": "Akira",
      "lastName": "Laine",
      "number": "0543236543",
      "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
      "firstNam e": "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 i=0;i<contacts.length;i++)
{
   if(contacts[i].firstName==name)
     {
       if(contacts[i].prop==undefined){return "No such property" }
       else if(contacts[i].prop!=undefined){return contacts[i].prop;}
   }
else {"No such contact"}}
// Only change code above this line
}

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/90.0.4430.212 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

I’ve edited my code .does it look good now?

I’ve formatted your code, maybe you can see there are issues with the syntax

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

for example you can’t put an else after a semicolon

Tell us what’s happening:
my code does not work when I run the test. I cam not find out the problem of my coade. please help me to debug

  **Your code so far**

// Setup
var contacts = [
  {
      "firstName": "Akira",
      "lastName": "Laine",
      "number": "0543236543",
      "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
      "firstNam e": "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 i=0;i<contacts.length;i++){
if(contacts[i].firstName==name){
  if(contacts[i].prop==undefined){return "No such property" }
  else if(contacts[i].prop!=undefined){return contacts[i].prop;}}
else{"No such contact"}
// Only change code above this line
}}

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/90.0.4430.212 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

It is so much easier to help if you format your code.

1 Like

If you right click on your mouse then there is a way to format your code in the FCC editor.

1 Like

I’ve resolved all the syntax error but still, it’s not working plz help

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

Please attempt to format your code. Someone already did it for you once. It really is hard to read with this lack of standard formatting and spacing.

done sir now plz help

function lookUpProfile(name, prop){
// Only change code below this line
  for (var i=0; i<contacts.length; i++) {
    if (contacts[i].firstName==name) {
      if (contacts[i].prop==undefined) {
        return "No such property"
      } else if (contacts[i].prop!=undefined) {
        // The condition is the exact opposite of the one above
        // That means you don't need an else if and can use an else
        return contacts[i].prop;
      }
    } else {
      // What are you doing here?
      // There are two problems with this
      // One is syntax and one is logical (when do you know no contact matches?)
      "No such contact"
    }
// Only change code above this line
  }
}

No. You did not format your code. I have done so and added a couple of comments.

1 Like

// Setup

var contacts = [

{

    "firstName": "Akira",

    "lastName": "Laine",

    "number": "0543236543",

    "likes": ["Pizza", "Coding", "Brownie Points"]

},

{

    "firstNam e": "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 i = 0; i < contacts.length; i++) {

    if (contacts[i].firstName == name) {

        if (contacts[i].prop == undefined) { return "No such property" }

        else{ return contacts[i].prop; }

    }

    else {return "No such contact" }

    // Only change code above this line

}

}

lookUpProfile(“Akira”, “likes”);

I’m trying to say if I find no contact what should i return

plz explain it a bit deeply :pleading_face:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.