Profile loopup code is confusing

Profile loopup code is confusing
0.0 0

#1

I tried doing this many times, in the course of 3 days, but it’s not working for me. I need help

here’s my code

//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(firstName, prop){
// Only change code below this line
 for (var i=0; i < contacts.length; i++){
   for(var j=0; j < contacts[i].length; j++){
     if (contacts[i][j] == contacts[i][firstName] && contacts[i][prop]){
       return contacts[i][prop];
     } else if(contacts[i][firstName] === false ){
       return "No such contact";
     } else if(contacts[i][prop] === false){
       return "No such property";
     }
   }
 }
// Only change code above this line
}

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

#2

Your first for loop looks good, but you don’t need the second one. You use for loops to iterate through arrays, but what is contacts[i]? Try this:

// ...
function lookUpProfile(firstName, prop){
// Only change code below this line
 for (var i=0; i < contacts.length; i++){
    console.log("contacts[" + i + "]" + " is " + contacts[i]);
  }
}
// ...

Then, open your console and take a look at what it says.


#3

I did some edits,
I only get one error now, and that’s in case of bob(no element exists on bob so it should return no contact)

function lookUpProfile(firstName, prop){
// Only change code below this line
 for (var i=0; i < contacts.length; i++){
   if (contacts[i].firstName == firstName && contacts[i][prop]){
     return contacts[i][prop];
   } else if(!contacts[i].firstName){
     return "No such contact";
   } else if(!contacts[i][prop]){
     return "No such property";
   }

  }
}

#4

the solution is to remove your “No such contact” return statement and place it below and outside of the for statement like this:

for (var i=0; i < contacts.length; i++){
   if (contacts[i].firstName == firstName && contacts[i][prop]){
     return contacts[i][prop];
   }  else if(!contacts[i][prop]){
     return "No such property";
   }
 }
   return "No such contact";

I dont know why your code wont work, my code for that challenge was different but it had the same fix that i typed just now. Lets just say this code will check the first two outcomes(firstName and prop having a match, only firstName having a match). If any firstName meets any of those two outcomes than the function will return a value before the “No such contact” return statement is executed, because a function stops when it executes a return statement. Then, if a firstName is specified but doesnt exist, it will exhaust all objects first for checking and if there isnt a match, than the for statement doesnt do anything and the return statement at the bottom is executed.


#5

Thanks alot! :smiley: It really helped me! I didn’t know about this logic wow