Profile lookup : loop doesn't seem to start

Profile lookup : loop doesn't seem to start
0

#1

Hi,

Here is my issue:
I tried several approach that should have worked and finally chose this one ( I checked other posts about this afterward:)
Still, no matter what I do, i end up with the same issue :
My loop doesn’t seem, to start or best case scenario go past the first iteration.
I figured that I have a problem with this part of the code but can’t find the mistake
Any help would be much appreciated !
(code below)

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




}
// Only change code above this line
}

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

#2

Did you try to debug this … as it pretty easy to find whats wrong if you debug … If you dont know how to debug … you really need to learn how to do it as it will make coding so much easier for you …
for this i copied your code and pasted into http://www.pythontutor.com … selected start visualizing your code … and then from the drop down … write code in … selected javaScript ES6

with this i can step through your code line by line …
so first problem is in for loop … for (var i; <<<< should be for(var i = 0;
so that sorted got looping …
next problem is … if (contacts.firstName … should be if (contacts[i].firstName
it then returned correct result … so I stopped checking any-further
as it either works … or if not … will be good practice for you to debug


#3

Hi,

Thanks for the tips and resource,
Will use that python tutor.
Thanks!

Edit: Yes I was able to sort it out and solve the problem, this is going to make my life So much easier.