0

For the below code. The following return the correct results:

lookUpProfile("Bob", "number"); // returns 'No such contact'
lookUpProfile("Akira", "address"); // return 'No such property'

However, for the following it doesn't return the expected result.

lookupProfile("Kristian", "lastName"); // should return 'Vos'
lookupProfile("Sherlock", "likes"); // should return ["Intriguing Cases", "Violin"]

Thank you.

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][prop] === prop) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    } else {
      return "No such contact";
    } 
  }
// Only change code above this line
}
1
  • 1
    lookupProfile("Kristian", "lastName"); must be lookUpProfile("Kristian", "lastName"); Commented Jun 12, 2017 at 8:30

3 Answers 3

3

You are trying to access the property like this contacts[i].prop

prop is a variable thus you need to use the brackets notation contacts[i][prop]

Another way of doing this :

You can use Array#find() to get the contact with the first name.

find() will return undefined if it doesn't find a contact. They you can access the property and see if it returns undefined or not

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) {
  const contact = contacts.find(c=>c.firstName === firstName);
  if(!contact) return "No such contact";
  const propValue = contact[prop];
  if(!propValue) return "No such property";
  return propValue;
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

And with a one-liner return

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) {
  const contact = contacts.find(c=>c.firstName === firstName);
  return contact ? contact[prop] ? contact[prop] : "No such property" : "No such contact";
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

And using a for-loop

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(let i=0; i<contacts.length; i++){
    if(contacts[i].firstName === firstName){
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }else{
        return "No such property";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Bob", "number")); // returns 'No such contact'
console.log(lookUpProfile("Akira", "address")); // return 'No such property'
console.log(lookUpProfile("Kristian", "lastName")); // should return 'Vos'
console.log(lookUpProfile("Sherlock", "likes")); // should return ["Intriguing Cases", "Violin"]

Sign up to request clarification or add additional context in comments.

2 Comments

Weedoze. Thank you for such a good solution. Is there a way to do it using for noobie syntax such as: 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][prop] === prop) { return contacts[i][prop]; } else { return "No such property"; } } else { return "No such contact"; } } // Only change code above this line }
@martinbshp No problem - You should check the built-in functions :) They can be very useful - map(), find(), filter(), reduce(),...
1

Correct code for getting your prop:

return contacts[i][prop]

When you do contacts[i].prop it's looking for {'prop': ...}

Corresponding docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Comments

0
for (var key in contacts) {
  //You can loop through the contacts object
  var obj = contacts[key];//get the object
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.