Help on Profile Lookup?

Help on Profile Lookup?
0

#1

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


Help on Profile Lookup
#2

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]


#3

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


#4

I don’t think you understand what you are attempting to push into the value array. The outer loop would represent the indices of the contacts array, but since there are only 4 elements, your for loop condition assumes there are 5 elements, The inner for loop would assume that each contact is an array and each contact is an object, so you can not use a for loop in that manner to iterate through an object.

The bigger question is, what kind of data values were you expecting value to contain after the loops above completed?


#5

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

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

#6

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’