Profile Lookup https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Tell us what’s happening:

Hi,

I’m not sure about naming my variables that I’ll use in my for loops. I know I want to go through the array of objects to check for the name and the prop. I think my instructions that I wrote for myself give a good idea of how im thinking about the problem. Thanks for feedback

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
//Name parameter is firstName, check to see if it's the contact's real first Name using a for loop
//if no, 'No such contact'

// prop is a parameter, check if prop is a property of the contact using a for loop
//if no, 'No such property'

//If both are true, return the value of the property
var property =prop;
var name = firstName;
for (var i=0; i <contacts.length; i++){
    if (name !== contacts[firstName]){
        return 'No such contact';
    }
    for (var i=0; i<contacts.length; i++){
    if(property !== contacts[name][property]){
            return 'No such property';
        }
    }
    return contacts[name][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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

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

var name = firstName;

Here firstName is undefined, so you are overwriting the parameter name with the value undefined.

1 Like

i changed it to var name =firstName; I am still confused about setting my parameters to new values. Seems like its the wrong thing to do.

I’m confused. var name = firstName; is what you were already doing. I was trying to point out to you why it is wrong.

I understand why it is wrong. I went ahead and looked at the solution to the problem. I see that the variable in the for loop (x,i,k, whatever variable i decide) is going to be the property when i write contacts[i];

Now I can use contacts[i] as my variable to check conditionals. I still need to work on adding a lot of other important pieces

What I usually do to help me simplify what’s going on is by extracting the current item in the loop iteration out of the collection first thing inside the loop:

// Get the current contact object
for (let i = 0; i < contacts.length; i++) {
    let contact = contacts[i];
    // rest of loop
}

…then you can:

contact.prop // dot notation - contact.firstName
contact[prop] // bracket notation - contact["likes"]

…etc throughout the loop body. I find this makes things easier to visualise, it might help you. Also should probably use let or const instead of var nowadays for variables.

1 Like

Thanks, now I am using the nested if statement to check if:

  1. the firstName matches
  2. The prop matches

here is how I think it should be written:
if (name ==firstName[contact]) && (if prop==contact[prop])
return contact[prop];

Your way to solve this problem is great, but check out some things:

I might be wrong, but you are re-writing the variable i to 0, since it is a global variable, try using another letter, plus, be careful, in that second loop you are counting the numbers of contact and going through the properties. You should count the properties of the contact and loop through it.
And as said before, you are giving the var name an undefined value.

Most important, you are counting the contacts, but you are not checking the contacts, actually; think about this; you are looping through an array with objects, so contact[0] will give you the first object.

Check out this previous lesson, could help you to remember something important:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects/

1 Like

Remember you can’t do this with variables

1 Like

:slight_smile:

Was just trying to show two ways of accessing properties (without highlighting which to use and why).

Maybe I could add some comments to make it clearer.

You can use backticks around your code to make it more legible. It’s just to the left of the Z on a UK macbook, not sure about other keyboards.

name == firstName[contact]

…you’re checking if name is the same as the property firstName on the contact object so this would be contact.firstName

Can’t have another if after the &&. So the code would be:

// should use triple equals
if (name === contact.firstName && prop === contact[prop])

… but this poses another problem. Let’s say the name AND the prop matches for this contact. So we’re good to go i.e. return contact[prop]. But what if the name matches but the property doesn’t? How are you gonna:

return 'No such property';

Maybe a structure something like this:

let contact = contacts[i];

// the name matches
if () {
    // ...and contact has own property 'prop'
    if () {
        // ...return the property
    }
    else {
        // ...return a string instead
    }
}

After the loop, you can set a ‘default’ return statement, which will run if nothing has been returned earlier i.e. the name doesn’t match, something like:

return 'No such contact';

1 Like

Hi Kurtony,

Thanks for your feeback. I did the exercise you sent, thank you.

I am very lost about ‘re-writing the variable i to 0’. I always have been taught to do a for loop starting with the declaration of let i=0; or var i=0. I dont understand why i is global? It’s in the for loop?

I made fixes after reading what you and what everyone wrote to me(thanks!) and this is what I came up with:

function lookUpProfile(name, prop){
// Only change code below this line
/*loop through the array of objects with a for loop. Use the for loop to count the properties of the contact and loop through it*/
/*set a variable 'contact' for each object which is contacts[i];*/
for (let i=0;i<contacts.length;i++){
    let contact=contacts[i];
        if(name===contact.firstName &&                  prop===contact[prop]){
            return contact.name && contact              [prop];
if(name!==contact.firstName){
    return "No such contact";
        }if (prop!== contact[prop]){
            return "No such property";
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes")

Thanks, swcarrey, I’ve read your feedback, thank you so much. I sent my most recent draft to Kurtony. I used backticks and got rid of the second if in my && conditional. On my last conditional where I use ‘else’, I’m not sure if i can get away with just saying else{
‘No such property’
}
Then I would be leaving out the conditional part of that else statement. I am not sure if I have to be more specific in my else statement to get it to say what i want or if all of the if conditions above make it so that the else statement does not need to explicitly state the condition.

function lookUpProfile(name, prop){
// Only change code below this line
/*loop through the array of objects with a for loop. Use the for loop to count the properties of the contact and loop through it*/
/*set a variable 'contact' for each object which is contacts[i];*/
for (let i=0;i<contacts.length;i++){
    let contact=contacts[i];
        if(name===contact.firstName &&                  prop===contact[prop]){
            return contact.name && contact              [prop];
if(name!==contact.firstName){
    return "No such contact";
}else if (prop !==contact[prop]){
    return "No such property";
}
// Only change code above this line
}
function lookUpProfile(name, prop){
// Only change code below this line
/*loop through the array of objects with a for loop. Use the for loop to count the properties of the contact and loop through it*/
/*set a variable 'contact' for each object which is contacts[i];*/
for (let i=0;i<contacts.length;i++){
    let contact=contacts[i];
        if(name===contact.firstName &&                  prop===contact[prop]){
            return contact.name && contact              [prop];
if(name!==contact.firstName){
    return "No such contact";
}else if (prop !==contact[prop]){
    return "No such property";
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes")
1 Like

@Kittles04 not quite there but getting closer for sure! But the code is still pretty hard to read, check this video out, at 0:35 seconds in, shows how to use backticks to insert code: https://www.youtube.com/watch?v=6nxjPtXHOYo

Use [one backtick]YOUR CODE[one backtick] for a single word or line

Use:

[three backticks]
YOUR CODE LINE 1
YOUR CODE LINE 2
YOUR CODE LINE 3
[three backticks]

…for multi-line/blocks

Edit your post and make it more readable then people can help easier :slight_smile:

1 Like

@Kittles04 Always try to helpyourself with pseudocode.

Loop through every contact, and know if:

   there IS that name, check if **THERE IS** the property
       if there is property, return name and property in an array.
       if there IS NOT property, return "no such property"

   there is not that name, then do nothing

if the loop finishes and nothing was found, return "no such contact"

Your code stops when checking the first object, that is because of the “return”, it breaks the loop, and in this case, finishes the function.
Other thing, in your code check by yourself the values of prop, and the value of contact[prop], youre are checking if they are different.
prop !== contact [prop]

Sometimes a thing that can help is writing the code again.

2 Likes

I’ve edited your post 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 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

1 Like