Profile Lookup
We have an array of objects representing different people in our contacts lists.
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.
The function should check if firstName is an actual contact’s firstName and the given property (prop) is a property of that contact.
If both are true, then return the “value” of that property.
If firstName does not correspond to any contacts then return “No such contact”
If prop does not correspond to any valid properties then return “No such property”
This is the solution:
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
I don’t understand how contacts.length matches with the other variable to allow you to look up a certain property and cycle through it. Can someone please explain what part the for loop has and what each part of it means (this includes the following - ([initialization]; [condition]; [final-expression])
More specifically, how do I know where to initiate the sequence? Does zero mean the sequence starts at firstName, for example?
What condition is it measuring as true or false
What is being plussed by 1 (i++)?
I really understood the numerical experssions (like add the odd numbers) but the word problems are totally different.
function lookUpProfile(firstName, prop){
var value;
for (var i = 0; i < contacts.length; i++){
if((contacts[i].firstName === firstName) &&
(contacts[i].hasOwnProperty(prop))){
value = contacts[i][prop];
break;
}
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
}
if (contacts[i].hasOwnProperty(prop) === false) {
value = "No such property";
}
}
return value;
}
I’m trying to walk through this code… I understand the concepts by themselves but it is different understanding a word problem.
if((contacts[i].firstName === firstName) &&
(contacts[i].hasOwnProperty(prop))){
value = contacts[i][prop];
break;
}
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
Is this saying that if the letters of the objects match the letters of the first name, the search will proceed? and then it goes on to other things. I don’t understand the .properties part too, but one thing at a time I guess.
So is it like sticky notes to yourself? Also, do you have a javascript editor that works when you run it? I’m having trouble adjusting the codepen.io javascript settings to allow what you sent me to actually run
But unlike sticky notes, it appears as part of the program but does not cause anything to change. Maybe more like a whiteboard (like notes) that you erase, like speech notes?
This is the solution from the answer page on free code camp (I got confused). This is what I originally posted to you.
if (contacts[x].firstName === firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
I know this is alot but I included the entire screen.
IT includes all the information I see.
Scroll to the bottom. I dont think calling the function was an issue because the function was called.
//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
function lookUpProfile(firstName, prop){
var value;
for (var i = 0; i < contacts.length; i++){
console.log(contacts[i].firstName === firstName, contacts[i].hasOwnProperty(prop));
if((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop))){
console.log('firstName of object matches firstName argument and object has a property with the same name as the value of prop');
value = contacts[i][prop]; // assign the value of the object's property with name equal to the value of prop to the value variable
break; // stop searching because you found a match
}
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
}
if (contacts[i].hasOwnProperty(prop) === false) {
value = "No such property";
}
console.log(); // line break
}
return value;
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
//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
var value;
for (var i = 0; i < contacts.length; i++){
console.log(contacts[i].firstName === firstName, contacts[i].hasOwnProperty(prop));
if((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop))){
console.log('firstName of object matches firstName argument and object has a property with the same name as the value of prop');
value = contacts[i][prop]; // assign the value of the object's property with name equal to the value of prop to the value variable
break; // stop searching because you found a match
}
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
}
if (contacts[i].hasOwnProperty(prop) === false) {
value = "No such property";
}
console.log(); // line break
}
return value;
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");