function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === firstName){
It looks like an issue with your what you’re comparing contacts[i].firstName to.
I’m having the same issue. In the instructions it says, ‘the function should check if name is an actual contact’s firstName’, but the function takes the argument name, not firstName. Is this a mistake or have I misunderstood something?
I wrote: if (contacts[i].firstName === name), but this doesn’t work.
Nothing is wrong with your code. The problem that you are not looking into is how you are calling the Array!!!
example:
let arr = [a, b, c];
To access values, you write,
arr[0];
arr[1]
arr[2]
Right!
Now, let us look into more complex data.
let arr = [{ a: 1, b: 2, c: 3 }];
Now to access the values how you should write?
arr[0]['a']
arr[0]['b']
arr[0]['c']
Now understand the difference between each of these and re-write your for loop to pass the test.
And also you have to fix the for loop, And you can add comparison values in if else statements using && and ||
And don’t forget to close the topic with a solution.
function lookUpProfile(name, prop){
// Only change code below this line
var value;
for (var i=0;i<contacts.length;i++){
if(contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)){
value = contacts[i][prop];
break;
}else if(contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)=== false){
value="No such property";
break;
}else if(contacts[i].firstName != name ){
value = "No such contact";
}
}
return value;
}
The first one didn’t work because let’s say you are calling the function with two arguments "Harry", and "lastName". Now during first iteration of the for loop it will get the first object with firstName with value "Akira". When it will reach the last else if it will compare "Akira" with "Harry" and since both doesn’t match the function will return "No such contact" and the for loop will terminate there and won’t check further. That’ts it. Let me know if something is not clear.