Reputation: 25
I'm trying to solve a challenge and here's the problem set
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"
My attempt
//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++){
if (contacts[i].firstName===firstName && contacts[i].hasOwnProperty(prop)===true){
return contacts[i][prop];
}
else if (contacts[i].firstName===undefined){
return "No such contact";
}
else if (contacts[i].hasOwnProperty===false){
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
If I enter non-existent values into the lookUpProfile function it just returns undefined
e.g lookUpProfile("Donald", "likes");
<. Undefined
Please help me fix this. You can also leave a comment on this gist
Thanks
Upvotes: 1
Views: 26667
Reputation: 81
I encountered this on FreeCodeCamp's Javascript Algorithms and Data Structures profile lookup. This is how I solved it
const 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) {
const found = contacts.find(c => c.firstName == name)
return !found ? "No such contact" : !found[prop] ? "No such property" : found[prop]
}
console.log(lookUpProfile("Akira", "address"));
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
Upvotes: 0
Reputation: 1
Came across this problem on FreeCodeCamp's Javascript Algorithms and Data Structures profile lookup problem myself and it seems the above answers are somewhat outdated.
Below is how I fixed it:
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false)
return "No such property"
} return "No such contact"
// Only change code above this line
}
Upvotes: 0
Reputation: 3
Another way of doing this is by using OR operators. Then, only one "if statement" is needed":
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) {
for (i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
return contacts[i][prop] || "No such property";
}
return "No such contact";
}
}
console.log(lookUpProfile("Akira", "likes")); // Returns [ 'Pizza', 'Coding', 'Brownie Points' ]
console.log(lookUpProfile("Akira", "current job")); // Returns No such property
console.log(lookUpProfile("Karanja", "likes")); // Returns No such contact
Here, the second part of the OR operator works as an "else" statement.
Upvotes: 0
Reputation: 31
you can just simply resolve it like this:
const found = contacts.find(contact => contact.firstName == name)
return !found ? 'No such contact' : found[prop] || 'No such property'
Upvotes: 1
Reputation: 3795
Try following code:
function lookUpProfile(firstName, prop){
// Only change code below this line
var isFirstName = false;
for(var i =0;i<contacts.length;i++){
if(contacts[i].firstName === firstName){
isFirstName = true;
}
}
if(!isFirstName){
return "No such contact";
}
else {
for(var i =0;i<contacts.length;i++){
if (contacts[i].firstName===firstName && contacts[i].hasOwnProperty(prop)===true){
return contacts[i][prop];
}
else if (contacts[i].hasOwnProperty===false){
return "No such property";
}
}
}
// Only change code above this line
}
// Change these values to test your function
console.log(lookUpProfile("Donald", "likes"))
Upvotes: 0
Reputation: 4099
Your first condition in loop should check if name is equal. If it satisfy Then you should check if the property exists If it exists return the value else return no such property
.If No such name is found then it will come out of loop and you can return no such contact
.
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++){
if (contacts[i].firstName===firstName){
if(contacts[i].hasOwnProperty(prop)===true){
return contacts[i][prop];
}
else {
return "No such property";
}
}
}
return "No such contact";
}
//No such contact
console.log(lookUpProfile("Donald", "likes"));
//No such property
console.log(lookUpProfile("Sherlock", "locks"));
//returns value of property
console.log(lookUpProfile("Kristian", "likes"));
Upvotes: 2