Profile Lookup - need help feedback

Profile Lookup - need help feedback
0

#1

**Tell us what’s happening:
What am I doing wrong w this coding challenge?

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++){
if (name===contacts["firstName"]&&prop===contacts[i].hasOwnProperty(prop)){
  return contacts[j][prop];
}
}

for (var i=0;i<contacts.length;i++){for (var j=0;j<contacts[i].length;j++){
    (name==contacts[j]){
  break;
} 
return "No such property";
}

}

for (var i=0;i<contacts.length;i++){for (var j=0;j<contacts[i].length;j++){
    if (prop==contacts[j]){
        break;
        {
            return "No such property";
        }
    }
}}

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

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


#2

You are making yourself complicated

if (name===contacts["firstName"]&&prop===contacts[i].hasOwnProperty(prop))

contacts[“firstName”] is wrong, you are inside a loop iterating an array and if you have to access the elements inside the array, you need its index. So contacts is the array and after looping through you must compare with contacts[i], and i which is the index value obtained from the loop and access the property using dot operator or []

Now into the test,

  1. The function should check if name is an actual contact’s firstName and the given property (prop) is a property of that contact.

a.) After the for loop you need to check if the name passed from argument is equal to the firstName of the item that is looped inside the array, make sure you have the index as i mentioned above.

b.) hasOwnProperty() will return a boolean value, so from the array[index] with a dot operator to hasOwnProperty(), pass prop as the parameter, now check the prop is true or false
Hint: a.) and b.) are nested if statements

If both are true, return the value else return what the instruction says

  1. If name does not correspond to any contacts then return “No such contact”

If first condition fails return this

  1. If prop does not correspond to any valid properties of a contact found to match name then return “No such property”

If second condition fails return this


#3

Tell us what’s happening:
Hi Sunit, thank you for feedback! I am trying to take your advice. Simply the code. I did this below. However, I am missing something. I do not know what.

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++){
if (name===contacts[i].firstName&&contacts[i].hasOwnProperty(prop)===true){
  return contacts[i][prop];
} else if(name!==contacts[i]){
return "No such property";
} else if (prop!==contacts[i])
        {
            return "No such property";
        }
    }
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

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


#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a 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.

    else if(name!==contacts[i]){
      return "No such property";
    }
    else if (prop!==contacts[i]) {
      return "No such property";
    }

Both of the above else if statements will always evaluate to false, because you are comparing a string to an object. Remember, that contacts[i] represents a specific element (which is an object) in the contacts array.

What where you thinking these two if statements were comparing?


#5

Tell us what’s happening:
I tried applying your recommendations but still not working.

an additional question - are these the same below?
contacts[i][firstName]
contacts[i][“firstName”]
contacts[i].firstName

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++){
if (name===contacts[i][firstName]&&contacts[i].hasOwnProperty(prop)===true){
  return contacts[i][prop];
} else if(name!==contacts[i][firstName] &&name!==contacts[i][lastName]){
return "No such property";
} else if (prop!==contacts[i][prop])
        {
            return "No such property";
        }
    }
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

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


#6

The above are the same, because they are referencing a property named “firstName” of a specific contacts object.

The following is something different. In this case, firstName should be a variable containing a string. If it is, then the following attempts to reference a property with the name contained in the variable firstName of a specific contacts object. However, since there is no variable named firstName, you will get a reference error.

contacts[i][firstName]

#7

The for loop returns the index of the array right, next

In the first if condition check for the firstName is equal to the name, if its equal, it will move on to the second if condition

In the second if condn, check if arr[i] hasOwnProperty(), it is boolean so it’s enough to place into the if(), if it is true return the value else return no such props

Try this syntax

for() {
    if() {
        if() {
        
        } else {
             
        } 
    }
}

#8

Hi Sujit! I don’t understand how switching from an && operator to two if statements will make a difference. What is your thinking?


#9

Hi Randall, I’m sorry. You leave me more confused. Can you give me a simple example to differentiate when to use case 1 and case 2?


#10

I could do that, but I think you should first review the previous curriculum challenges (see below) which explain the differences and when to use them.

Accessing Object Properties with Dot Notation

Accessing Object Properties with Bracket Notation

Accessing Object Properties with Variables


#11

Seems like using brackets at all times is best and then just putting quotes if it is 2 words.


#12

Most prefer dot notation, except when you must use bracket notation (i.e. when the property name is a variable or when the property has a space in the name). Dot notation is cleaner, because it is a single extra character instead of four extra characters [""]


#13

It does make a difference based on the question,

According to your code, if the name or prop is not equal, then the result is fine, but

Consider 5 objects in which the 5th obj has name and props as equal, so based on your condn, when the loop runs on the first obj, it will output the result as not matched because you have directly applied the condition for the obj which is equal inside the loop, so before the loop is incremented the output is thrown as not equal which is the wrong output.

The outputno such contact must be outside the loop, else for the obj which will be equal at last position, will be output as not equal even in the first iteration


#14

How about if I change the it to:
else if (name!==contacts[i][firstName] || name!==contacts[i][lastName]){
return “No such property”;
}

using the || or operator, rather than the && and operator?


#15

Tell us what’s happening:
So, this is what I have so far (below). Am I getting the right idea or am I missing just one small thing?

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++){
    if (name==contacts[i][firstName] && contacts[i].hasOwnProperty(prop)===true){
        return contacts[i][prop];
    } else if (name !==contacts[i][firstName] || name!==contacts[i][lastName]){
        return "No such contact";
    } else if (contacts[i].hasOwnProperty(name)===true && contacts[i].hasOwnProperty(prop)===false){
        return "No such contract";
    }
// 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.3; 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


#16

I think before you write any more code, you need to write out the steps you would take if solving this on paper. This will allow you to get your logic written first without trying to also figure out the correct JavaScript code to write. Try writing out your algorithm and post it here so we can make sure you have the general flow of logic correct first.

Think about what the first question you would ask while looking at each object in the array. Think about the second and third questions. The order of these questions will be important as they will affect the logic flow of the code.


#17

As @randelldawson suggested you could think the logic flow first, your code gets correctly executed for the items that are not equal to name or prop, but it doesn’t evaluate for items which are equal.

Your first if condition executes only if the first index of the arr is equal to name and prop, what if the arr which is equal is at the middle or last and if you use || it’s still the same, try your logic flow and make sure you think through the loop rather than only for the condn that satisfies the qn


#18

Logic Flow Explanation:
Arg1:
contacts[i][firstName] === name
Arg2:
contacts[i].hasOwnProperty(prop)

There are 4 possibilities:

  1. True, True
  2. True, False
  3. False, False
  4. False, True

In these possibilities:

  1. True, True ==> should return contacts[i][prop]
  2. True, False ==> should return No such property
  3. False, False ==> should return No such contact
  4. False, True ==> should return No such contact

I think conceptually my “for” loop & if/ else if statements should work.
However, I don’t think the “.hasOwnProperty” function was built to evaluate the name, whether the name is true or false. Rather, it was built to evaluate the prop or properties in this function, whether prop matches what is in the var contacts array.

I am studying the “Get a hint” solution. This if/if/else conditional statement construction is much simpler, and a good lesson for me. As simpler is better :slight_smile: I notice that the if/if/else conditional statement goes through all of the records before confirming that the whole system has no record of the first Name before returning “No such contact”. I can see how this is a very good way to do this.

I guess my next question from this is:
How does the for loop i++ counter move through the if/if/else statements?

I suppose it just moves sequentially through each first name record.


#19

At first the index will be 0, now
It enters the if condn checking the name value of index 0 is true or not, if its false it exits the condn and increments the loop to 1, it is repeated until the condition is met,

Based on your theory, lets say the possibility is true and true, and what if the position of that possibility is at the last index.? In that case when the loop checks the values for first index the value for both name and prop becomes false therefore the condn gets terminated and the result becomes no such contact, but the result should have returned the value for the possibility item

if its true, it enters the second if condn to check the prop value to be true, if it is false, then no such prop is returned


#20

The way I understood this exercise was like this:
We start the loop through the entire array of objects.
first we check if the “name” is in the contacts list. If yes, then we check for if the contact has “prop”. If true, we return the value. If “prop” is not there. We return 'No such property" . If the “name” is not there. We exit the loop and return No such a contact. No reason to check for anything else.
So here is a psuedo code

For contacts iterate {
IF name is in contacts THEN check for prop {
IF prop is TRUE {
return the contacts property value
}
else prop is False {
return No such property
}
HERE we EXIT the loop. No reason to check for anything
}

we return Contact not found. Since the first IF statement is FALSE when name is not found in contacts.