Profile lookup
See original GitHub issueHi, I’m not sure this is a bug but I asked in help forum and was told that the code is right and it should work.
So this is my code:
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 (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===true) {
return contacts[x][prop];
} else if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===false) {
return "No such property";
} else if (contacts[x].firstName !== firstName && contacts[x].hasOwnProperty(prop)===true) {
return "No such contact";
}
}
}
The code works well with both “else if” statements, when either firstName or prop are false; but it doesn’t work when both are true. I even tried creating a new “else if” statement to cover the case where both conditions are false, but of course it doesn’t work either.
Thank you for your help.
Edit (by @raisedadead)
Challenge Name : Profile Lookup
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
freeCodeCamp Challenge Guide: Profile Lookup
Profile Lookup · Hint 1. Use a for loop to cycle through the contacts list. · Hint 2. Use a nested if statement...
Read more >CodeChallenge/Profile Lookup.md at master - GitHub
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you. The function should check if firstName...
Read more >trying to understand this profile lookup in JavaScript
I have used two different variables to track the missing conditions here. function lookUpProfile(name, prop){ // Only change code below this ...
Read more >UserSearch: Find User Profiles | Dating Sites | Free Search
600+ sites Supported! Largest Reverse User Search Online! Find someone by username or email. Premium Reverse Lookup Tool FALL SALE : 75 ...
Read more >JavaScript Algorithm: Profile Lookup | by Erica N - codeburst
We write a function that will look through an array of objects and return the value of a particular property if certain rules...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
mine doesnt even loop …after i have initialize person to 0 it doesnt increment accordingly
###This_Snippets_seems_great
// 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(name, prop) { // Only change code below this line for(var x=0; x<contacts.length; x++){ if(contacts[x][“firstName”]==name){ if(contacts[x].hasOwnProperty(prop)===true){ return contacts[x][prop]; }
// Only change code above this line } var a = lookUpProfile(“Harry”, “likes”); console.log(a)