Why I write the code like below doesn't work?

Tell us what’s happening:
Why I write the code like below doesn’t work?

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 i=0; i<contacts.length; i++) {
    for(var j=0; j<contacts[i].length; j++) {
        if(name==contacts[i].firstName && prop==contacts[i][j]) {return contants[i][prop];}
        if(name !== contacts[i].firstName) {return "No such contact";}
        if(name==contacts[i].firstName && prop !==contacts[i][j]) {return "No such property";}
    }
}
// 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Profile Lookup

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

I am not sure, but you are trying to access an element which is like 2D array, here we have array of objects.

For example:

var contacts = [
{
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
},
{
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
}];

console.log(contacts[0]); 
//  Output as { firstName: 'Akira',
//  lastName: 'Laine',
//  number: '0543236543',
//  likes: [ 'Pizza', 'Coding', 'Brownie Points' ] }

console.log(contacts[0][0]);
//Undefined 

try using bracket notation to acces property inside object

1 Like

this is my code

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

// Change these values to test your function
lookUpProfile("Bob", "number");

When you want to access the obj key and value use obj["name] or obj["clothing"]["style"].

In the above image you are trying to print propt i.e a key of obj object.

Can you paste the code here ?? Full code pls

sorry, there is confusion here

I think it is prop == contacts[i][j] that doesn’t work as you think
prop is a property name, where contacts[i][j] is a property value, they can’t be equal

you also have a few other issues, but I think you are missing a bit of practice in working with objects

prop==contacts[i][j] is undefined

What is mean is when you console.log(contacts[i][j]) your output will be undefined. .Now you are comparing undefined with prop. So your code does not work.
Reason is j start from 0 to length, but it is not storing object like array. So u can’t access it.

let arr = [ [1,2,4], [5,6,7,] ];
here you can access it using arr[0][0]; because it is stored like that

j is the variable of a for...in loop, it is iterating on the property names

Yes i was talking about this code

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

Don’t be confused, try to write a program on paper first and then write on fCC editor

a, yeah, please @hahaha123, paste your last code (instead of a screenshot) so it is possible to see what you have changed

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

sorry everyone, i will be back.
My mom is calling me.
see you later :blush: