Help on Profile Lookup?

I just started on the for loop but I don’t think my for loop is right. Here’s what I done. Anything I should look into that I’m doing wrong?

var value = [];

for (var i = 0; i < 5; i++) {
    for (var j = 0; j < 5; j++) {
        value += contacts[i][j];
    }
}

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

Considering your loop described above, REGARDLESS of what the task is asking, you are trying to populate
the array ‘value’, with the result of what the for loop is producing. In this case( considering your loop only ), you should use: value.push( contacts[i][j] ) INSTEAD OF value += contacts[i][j]

1 Like

Alright I’m on the profile lookup question and I currently don’t have any of the task ticked off but what I’m struggling with is the if statement and probably the loop as well. I don’t know how you can loop through the contacts list and take the information you ask for. Is there any resources I can look into or a hint on what I’m doing correct or wrong so I can figure it out by myself?


//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

var value = [];

for (var i = 0; i < 5; i++) {
    for (var j = 0; j < 5; j++) {
        value.push(contacts[i][j]);
    }
}

if (contacts[i][j].hasOwnProperty(name) && contacts[i][j].hasOwnProperty(prop)) {
    return value[name][prop];
} 

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

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

To answer the last question I was hoping the function to output their likes. So like this

{
 ["Pizza", "Coding", "Brownie Points"]
    },

Hello Champ, I believe you are getting slightly confused iterating over the outer array elements. It is a matter of what is the data structure inside the array. In this case, the data inside the outer array is structured in the form of object literals { }. Consider the outer array first being just like: [ { }, { }, { }, { }, { } ] Each of its elements is an object literal. Therefore, in order to be able to access anything useful inside the array you need a way to ‘talk’ its elements and its elements are { } and objects they have their own data structure in form of properties and values, you need to access.
var testray = [‘hello’, ‘world’] , console.log( testray[0] ) // ‘hello’
var testray = [ { hello:‘hello’ }, {world:‘world’} ] , console.log( testray[0].hello ) // ‘hello’