Profile lookup challenge: why this code is getting errors

Hi, I’m redoing the JavaScript curriculum and got up to this challenge. For me the code is fine, I don’t understand why it is getting errors (not syntax errors, working errors). If someone can please help me here I’d be thankful.

// 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
if (name === "firstName" && name.hasOwnProperty(prop)) {
    return prop;
} else if (!name.hasOwnProperty(prop)) {
    return "No such property";
} else if (name = undefined) {
    return "No such contact";
}
}
// Only change code above this line


lookUpProfile("Akira", "likes");
function lookUpProfile(name, prop){
// Only change code below this line
  // Will the name ever actually be 'firstName'?
  // Will the string have a property?
  if (name === "firstName" && name.hasOwnProperty(prop)) {
    return prop;
  } else if (!name.hasOwnProperty(prop)) {
    return "No such property";
  } else if (name = undefined) { // This assigns 'undefined' to 'name'
    return "No such contact";
  }
}

I don’t think this syntax means what you think it does.

I’m not exactly sure why you are comparing name to the string "firstName" or checking if the string name has the property prop.

I think you are missing how to iterate over an array of objects in your code.

1 Like

@JeremyLT I understand what you’re telling me. I’ll review some things first before I continue. Thank you.

1 Like

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


lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 6.0.1; SM-G532M Build/MMB29T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

you may want to review the difference between bracket notation and dot notation and when to use each one

2 Likes

I have added a few comments

1 Like

Thank you @ilenia and @JeremyLT. I’ll see to that. And sorry for the duplicate post. It won’t happen again.

@JeremyLT I added var before i = 0 and replaced the dot notation with bracket notation. I tried with and without name. I keep getting the same errors. The most striking is I get an uncaught syntax error about regular expressions (?). Maybe some little spelling mistake we didn’t see? Thank you so much!

What is your current full code?

I got your code to pass with 3 changes.

Mmm this is my first time on a cell phone and I don’t know how to copy yet though I tried to.

you can use the ask for help button, and instead of create a new topic, select all and copy then delete, and paste in a new post here

1 Like

Great! I’ll do that. Thank you.

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

@JeremyLT here it is

use == instead of = inside if…else statement
You wrote:
if(name=undefined) which is wrong.
if(name==undefined) is correct

This code you posted working fine for me.

1 Like

@kassahun That’s old. Thanks.

@JeremyLT OK, I’ll check it out again. A million thanks!

@JeremyLT Funny. The same code is not working for me. Tomorrow I’ll check it character by character. Thank you.

shrug I copy-pasted your full code in the last post:
image

@JeremyLT A curly brace was extra in my page. Problem solved.

1 Like