Look Up Profile

I do not know the reason why my code is not working


function lookUpProfile(name, prop){
// Only change code below this line

for (var i = 0; i < contacts.length; i++) {
   //console.log(contacts[i]);
     if(name=="first name"&&prop==contacts[i]){
            return prop;
             if (name!==prop) {
                   return "No such contact";
                   if (prop!==contacts[i])
                       return "No such property";
                   
            }
     }

}
// Only change code above this line
}

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

From first glance it appears your trying to use a variable, contacts, that hasn’t been assigned any value.

1 Like

all your if statements are nested in each other. this is your first biggest logical error

if this is true: name=="first name"&&prop==contacts[i]

this is what is executed:

most of your code is executed only if that first condition is true
plus, the first line is a return statement, which stops the function, so everything after is not executed

after this, you are also checking the wrong things
name !== prop is always true, as one is the name of the contact and the other is the name of the property you need to give the value of
prop !== contacts[i] the first is a string the second an object, this comparison doesn’t have any sense
( also name=="first name" where name will never be the string "first name", you may want to review how to access object properties)

there are also a few other things, but if you fix these is a good start

1 Like

I am trying to build the first condition, but I do not know how to iterate in specific parts of contacts. What may I do?

function lookUpProfile(name, prop){
// Only change code below this line


if( name == "firstName" && prop=="contacts"){
         return value;
}


// if  (firstName!==)  {

return "No such contact";

 //}

 if (prop!==){

//return "No such property";

 //}
// Only change code above this line

}

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

You might want to review the challenges dealing with iterating through arrays. contacts is an array of objects. Each object represents a person.

1 Like

What is the reason why this does not work??

//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 &&prop== contacts ){
                
                return contacts;

    }
    else if  (name  !=  contacts) {
         
         return "No such contact";
    }

    else if (prop != contacts){

         return "No such property";
    }
}
// Only change code above this line
}

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

Remember that as soon as JavaScript sees a return statement, the value specified is return and the function is exited. Based on the way you have written your code, in the first iteration of the contacts array, you are returning a value and then the function exits and stops iterating. Think about when you really should return certain values.

1 Like

contacts is an array of objects, it will never be equal to a variable that has a value of string
you may want to review how to access array elements and object properties.

1 Like
//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[0] &&  prop== contacts[0] ){
                
                return contacts;

    }
    else if  (name  !=  contacts) {
         
         return "No such contact";
    }

    else if (prop != contacts){

         return "No such property";
    }
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
console.log(contacts[0]["lastName"]);

I am trying to access to firstName exclusively for all cases, but I know how to do it for a case. How may I do that?

//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"] &&  prop=== contacts[i]){
                
                return contacts[i];

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

    else if (prop != contacts[i]){

         return "No such property";
    }
}
// Only change code above this line
}

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

I think I can access to every cell of the array, but I do not still make this work completly.

You have a few issues, but let’s handle the one below first.

contacts[i] is an object, so prop (which is a string) will never equal to contacts[i], so this statement will always evaluate to false. What are you trying to check in this first if statement? Also, what are you trying to return in the following line?

return contacts[i];

I ask because currently you are just returning an object.

1 Like

I am trying to accomplish the first condition: I want to return the value of that property if both conditions are done.

//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"] && prop ===contacts[i][prop]  ){
                
                return contacts[i][prop];

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

    else if (prop !== contacts[i][prop]){

         return "No such property";
    }
}
// Only change code above this line
}

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

I think I fix that error, it does not still work.

this is correct, nice

but

here you are checking if the prop variable, which would be a property name, and the value of a property are equal

also, be careful with this one:

what happens if name is "Sherlock"?
when you have "Sherlock" !== "Akira" this will execute, as that is the value of contacts[0]["firstName"], and they are
not equal to each other

1 Like

What kind of documentation can I read to solve it?

in this case it’s more a matter of algorithm solving
you need to reason and find a different way
I will tell you that if you put a return statement inside a loop and the return statement always execute it is like you don’t have a loop as it will stop at first iteration

try looking at your code with this tool, look at the tests that pass then one by one the tests that do not pass and try to find out how the things need to be changed
http://www.pythontutor.com/javascript.html

1 Like
//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"]){
                            
                           if(prop=contacts[i][prop]) {
                                        
                                     return prop=contacts[i][prop];

                            }

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

}
// Only change code above this line
}

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

What is the reason why this does not work?

You need to review the difference between assignment operator and comparison operator - here can lay all the difference between working and not working

If you have made so that inside a loop a return statement will always execute (like it happens when you have an else statement) it is like the loop doesn’t exist because at first iteration one of the return statements is executed, the function is exited having an output and the loop stops at first iteration

prop==contacts[i][prop] this is comparing the value of a certain prop with the name of a certain prop - could they be equal?

//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"]){
                            
                           if(prop===contacts[i][prop]) {
                                        
                                     return prop===contacts[i][prop];

                            }

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

}
// Only change code above this line
}

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

I am sorry, I wanted to mean this.