1

We have an array of objects representing different people in our contacts lists.

A lookUp 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 code, I have been stuck for a few hours on this one now :(

  //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": ["Intruiging Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    },
];


function lookUp(firstName, prop){
// Only change code below this line
for(i = 0; i < contacts.length; i++) { 
  if(firstName == contacts.firstName && contacts.hasOwnProperty(prop)) {
    return contacts.firstName + "" + contacts[prop];
  }
  else if(firstName !== contacts.firstName) { 
    return "No such contact";
  }
  else if(contacts.hasOwnProperty(prop) === false) { 
    return "No such property";
  }
}
// Change these values to test your function
lookUp("Akira", "likes");
3
  • Is this a homework assignment? Commented Jan 14, 2016 at 20:57
  • Ah, okay! Now that you say that I think I remember this one actually. What specifically are you having issues with? Commented Jan 14, 2016 at 20:58
  • Accessing array elements (MDN) Commented Jan 14, 2016 at 20:58

10 Answers 10

3

This is probably what you want to achieve

function lookUp(firstName, prop){
 for (var i = 0; i < contacts.length; i++){
  if (contacts[i].firstName === firstName) {
    if (contacts[i][prop]) {
        return contacts[i][prop];
    } else {
        return "No such property";
      }
    }
  }
  return "No such contact"; 
}
Sign up to request clarification or add additional context in comments.

Comments

1
function lookUp(firstName, prop){
// Only change code below this line
var entry = contacts.find(function(d){return d.firstName === firstName;});
return entry ? ( entry.hasOwnProperty( prop ) ? entry[prop] : 'No such property' ) : 'No such contact';
}

Comments

1

You may try es6 solutions by the way

function lookUp(firstName, prop){
  return contacts.find(x => x[prop] === firstName);
}

looks so tiny compare to other advices ;)

Best regards

Egor

Comments

0

You could just try to filter the object, and then check the length and property. No for loop needed.

var contacts = [{...}...]; // just an array of objects...

function lookUp(firstName, prop) {
    var nameMatches = contacts.filter(function(contact) {
        // im using toLowerCase to make the case not a factor in finding a match
        // you may not want this, and if you don't just remove it.
        return contact.firstName.toLowerCase() === firstName.toLowerCase(); 
    });
    if (nameMatches.length === 1) {
        if (nameMatches[0].hasOwnProperty(prop)) {
            return nameMatches[0].firstName + " " + nameMatches[0][prop];
        } else {
            return "No such property";
        }
    } else {
        return "No such contact";
    }
}

Comments

0
function lookUpProfile(firstName, prop){
    for( var i = 0; i < contacts.length; i++){
        if(firstName === contacts[i].firstName){
            if(contacts[i].hasOwnProperty(prop)){
                return contacts[i][prop]
            }
            else {
                return 'No such Property';
            }
        }

    }
return 'No such contact';
}

Comments

0
function lookUpProfile(firstName, prop){


  for(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(prop)===false)
        {
          return "No such property";
        }

   }
  return "No such contact";
}

this works for me....

1 Comment

You should try to explain why the code is broken, and how your solution works. Also, when formatting code you need to indent the entire block with 4 spaces, see help center for formatting.
0

This Code Satisfies all the 5 Conditions...

function lookUpProfile(firstName, prop){

  var a=0;
  for (var i = 0; i < contacts.length; i++) {

      if (contacts[i].hasOwnProperty(prop)) {
        
        a++;
        if (contacts[i].firstName == firstName) {
          
          return contacts[i][prop];
        }
        }
  }
     if (a===0)
     {
        return "No such property";
      }
     else{
       return "No such contact";
     }
}

1 Comment

please fix the code indentation, right now the opening and closing brackets are all over the place.
0
Another way to kill the cat

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

Comments

-1

You can try following logic

// Gets the array of object that matches the first name
var filteredByFirstName = contacts.filter(function(item){
    return item.firstName === firstName
});

// If the length of array is 0 means no contacts
if(filteredByFirstName.length === 0) {
    return "No such contact";
} else {
   // If contact found, then check for the property
   var filteredByProperty = filteredByFirstName.filter(function(item){
      return item.hasOwnProperty(prop)
   });

   // If no property found, then return no property found message
   if(filteredByProperty.length === 0) {
      return "No such property";
   } else {
      // Otherwise return the result  
      return filteredByProperty[0].firstName + "" + filteredByProperty[0][prop];

   }
}

1 Comment

@downvoter - I will be glad if a reason for down vote is specified
-1
function lookUpProfile(firstName, prop) {
    for (var i=0;i<contacts.length;i++) {
        if (contacts[i].firstName===firstName) {
            if (contacts[i][prop]) {
                return contacts[i][prop];
            }
            else {
                return "No such property";
            }          
        }             
    }
  return "No such contact";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.