Hello everybody. I don't understand why for loop no work. when i change var i for it work. I need your help . thanks very much

Tell us what’s happening:

Your code so far

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=2; i < contacts.length ; i++){
if(contacts[i].hasOwnProperty(name) || contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
}else if(contacts[i].hasOwnProperty(name)=== false){
    return "No such contact";
    return "No such property";

// Only change code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/80.0.180 Chrome/74.0.3729.180 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

if your i starts at 2 you are not checking the first and second element in the array

anyway, let’s imagine you are checking all the array
and that this is the function call
lookUpProfile("Kristian", "lastName")
the expected output is "Vos"

this is what the loop execute for each i

when i is 0, contacts[i] is:

    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]

so let’s see what the code does…
the first thing it checks is:

contacts[i].hasOwnProperty(name) || contacts[i].hasOwnProperty(prop)

name and prop are variables, which values, in this case of "Kristian" and "lastName"
so replacing the variables with their values this becomes

contacts[i].hasOwnProperty("Kristian") || contacts[i].hasOwnProperty("lastName")

the first one returns false, as "Kristian" is a property value, not a property name
the second instead returns true as contacts[i] has a property called lastName
so it becomes false || true which evaluates to true
this if statement execute!
let’s see what’s inside it

return contacts[i][prop]

if we do as before and replace variables with their values this becomes

return contacts[0]["lastName"]

the value of this property is “Laine” so it becomes

return "Laine";

the function has an output, but it is not the right one

some hints to get better:
if you want to check if this is the right object you need to compare the value of the firstName property with the name variable, don’t use hasOwnProperty for that
also, if both statements need to be true you can’t use ||
get better, fix with issue, and if needed we will try again in seeing what the code is doing step by step

1 Like

i’m thanks very much! I got it