Skip to content

Instantly share code, notes, and snippets.

@badarshahzad
Created June 27, 2018 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badarshahzad/f6567e43b13336a4ed5e9d986fa405c4 to your computer and use it in GitHub Desktop.
Save badarshahzad/f6567e43b13336a4ed5e9d986fa405c4 to your computer and use it in GitHub Desktop.
Basic JavaScript: Profile Lookup We have an array of objects representing different people in our contacts lists. A lookUpProfile function that takes name and a property (prop) as arguments has been pre-written for you. The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.…
//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 printValues(){
for(var a = 0; a< contacts.length; a++){
contacts[a].firstName;
}
}
function isNameExist(name ){
for(var a = 0; a< contacts.length; a++){
if (contacts[a].firstName == name)
return true
}
return false;
}
function isPropertyExist(prop){
for(var a = 0; a< contacts.length; a++){
if (contacts[a].hasOwnProperty (prop))
return true
}
return false;
}
function lookUpProfile(name, prop){
// Only change code below this line
if(!isNameExist(name)){
return "No such contact";
}else if(!isPropertyExist(prop)){
return "No such property";
}
for(var a = 0; a< contacts.length; a++){
if(contacts[a].firstName == name && contacts[a].hasOwnProperty(prop)){
return contacts[a][prop];
}
}
}
// Only change code above this line
// Change these values to test your function
lookUpProfile("Akira", "likes");
@Newton-Maurya
Copy link

// 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"]
}
];

var result
function lookUpProfile(name, prop){
// Only change code below this line
result.pop()
for (var obj of contacts) {
if(obj.firstName === name){
if(obj[prop]){
result.push(obj[prop])
}
else {
result.push('No such property')
}
}
}
return result[0] !== undefined ? result[0] : 'No such contact'
// Only change code above this line
}

lookUpProfile("Akira", "likes");

@dorutuu
Copy link

dorutuu commented Sep 12, 2023

function lookUpProfile(name, prop) {
for (let x = 0; x < contacts.length; x++) {
if (contacts[x].firstName === name) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment