Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
const 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 (i = 0, i < contacts.length, i++) {
  if(name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop)) {
   return contacts[i][prop];
   
   } 
  
  else if (name !== contacts[i].firstName)  {
      return "No such contact";
    } else if (contacts[i].hasOwnProperty(prop) === false) {
      return "No such property";
    
  
  // Only change code above this line
}
 }
  }

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

fSyntaxError: unknown: Missing semicolon. (31:37)

29 | function lookUpProfile(name, prop) {
30 | // Only change code below this line

31 | for (i = 0, i < contacts.length, i++) {
| ^
32 | if(name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop)) {
33 | return contacts[i][prop];
34 |

Hello, can someone tell me why I get this syntax error? I googled it but nothing makes sense.

the original code said not to change anything below a certain point, but you have added extra lines with braces:
Original code ended like this before:

  // Only change code above this line
}

lookUpProfile("Kristian", "lastName");

and yours is like this:

ok true. I changed that now. I still get the same error.

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

lookUpProfile("Akira", "likes");

SyntaxError: unknown: Missing semicolon. (31:36)

29 | function lookUpProfile(name, prop) {
30 | // Only change code below this line

31 | for (i = 0, i < contacts.length, i++) {
| ^
32 | if(name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop)) {
33 | return contacts[i][prop];
34 | }

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

you are still modifying the code below the line.
(Look at my first response to see how the code used to look and make it look like that again before trying to do any debugging)

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

lookUpProfile("Akira", "likes");```

like this?

yes, the reason for your ; error is your for loop
(check the syntax again, it should have ; not ,)

1 Like

Thank you that worked. Maybe you help me also with my solution. I am not sure why it doesn’t work. emphasized text

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

lookUpProfile("Akira", "likes");```

the first if statement doesn’t pass the tests. 2nd and 3nd do. I figured that there must be a mistake in how I access the value of the given property to return it. I tried some other stuff, bus nothing worked so far.

I also tried:

return contacts[i].prop```

return contacts[i][prop] 

return contacts[i].hasOwnProperty(prop)

in English your if statement is saying this:

if the given name value is equal to the current contact’s first name value
AND
if the given property value is equal to the boolean value of whether the property exists or not then…

2nd if will never be true since the test is never passing as prop a boolean

ah ok I see. So I don’t check if it s equal to true. I just check if the property exists, like you did in the code

for (i = 0, i < contacts.length, i++) {
  if(name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop)) {
   return contacts[i][prop];
   } 

i’m sorry I didn’t understand your understanding?
:smiley:

I am saying that you shouldn’t be comparing prop to the boolean value returned by the function hasOwnProperty as they will never be the same.

sorry for that.

It looks like you changed my code. Is that the solution?

my code is this:

if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop) == true)

and you changed it to 

if(name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop))

So since the .hasOwnProperty method returns a boolean value I can not set it equal or unequal to true or false? so what do I do?

Can this connection of if statement even work. I saw in the solution its recommended to use a nested if statement.

I didn’t touch your code. I quoted your code which you posted.

You can clearly see it is a quote because it has your user id in the quote block (top left corner).

I don’t write solutions for campers. I provide guidance/tips/hints.

We seem to be talking at cross-purposes.

If you would like to to reset this conversation, that would probably be best.

Let’s start by you posting what your current code looks like (again), and hopefully we can go from there.

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

lookUpProfile("Akira", "likes");

If you try to look for Kristian and firstName as prop, the first position 0 of the array is examined first, so in that case we will go to the first if statement and fail…

Then we will got to the second if and succeed, (Kristian is not Akira)
So we will return no such contact which is wrong…

Just add some console.log statements in your clause blocks and you can see how it runs…