pandeee
pandeee

Reputation: 35

Looping through an array with objects to retrieve properties

I'm working on Free Code Camp's curriculum and am stuck on a certain project. I don't think my loop is working correctly, because it's not retrieving the information it's supposed to.

The instructions are as follows:

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"

//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 (i = 0; i < contacts.length; i++) {

      if (contacts[i].hasOwnProperty(firstName)) {
        if (contacts[i].firstname === prop) {
          return contacts[i].firstName;
        }
        else {
          return "No such property";
        }
      }
      else {
        return "No such contact";
      }
  }
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

I'm not sure where my problem lies unfortunately, any help is much appreciated.

Upvotes: 1

Views: 1214

Answers (6)

Vishal Biswas
Vishal Biswas

Reputation: 1

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

Upvotes: 0

SangyK
SangyK

Reputation: 839

Rohit's answer is correct in most cases.

If I run the test case,

var data = lookUpProfile("Sherlock", "likes");   

it would return

"No such contact"

which is not the expected result.

The problem is with the 'else' part of

if (contacts[i].firstName == firstName) {}

While the first item in the 'contacts' is checked, return the value only when a matching contacts is found. Else ignore it and loop through ll the items in the 'contacts'. Once everything is done, we can return 'No such contacts'

I have modified the function a bit like below - try this,

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

var data = lookUpProfile("Sherlock", "likes");
console.log(data);

Updated JSFiddle - https://jsfiddle.net/z5ww2nbv/

Upvotes: 0

Amit Saxena
Amit Saxena

Reputation: 216

It is best to check for the property first, as in Rohit's answer. If you would like to check for the name first, you could try this, although less elegant:

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

Upvotes: 0

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27192

Try this it will work :

function lookUpProfile(firstName, prop){
// Only change code below this line
  for (i = 0; i < contacts.length; i++) {

      if (contacts[i].hasOwnProperty(prop)) {
        if (contacts[i].firstName == firstName) {
          return contacts[i][prop];
        }
        else {
          return "No such contact";
        }
      }
      else {
        return "No such property";
      }
  }
// Only change code above this line
}

// Change these values to test your function
var data = lookUpProfile("Akira", "likes");
console.log(data);

Working fiddle : https://jsfiddle.net/yp8b2tg7/

All requirements are fullfilled :

  • 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"

Upvotes: 1

abhiagNitk
abhiagNitk

Reputation: 1067

Please use firstName inside quotes when you are checking for hasOwnProperty. Moreover, JavaScript is a case-sensitive language. firstName and firstname are considered different variable. You are using if (contacts[i].firstname === prop) in your code whereas it should actually be if (contacts[i].firstName === firstName). And, Please compare the right property as well. You were comparing firstName to props, which I don't understand what you actually want to do.

//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 (i = 0; i < contacts.length; i++) {

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

Upvotes: 1

Gerard van Helden
Gerard van Helden

Reputation: 1602

There are several things not right with your code.

First, the hasOwnProperty does not do what you think it does. Read up on the docs on that

Furthermore, Javascript is case sensitive, and reports undefined properties to be value of type undefined. Therefore the following if does not do exactly what you want, without yielding an error.

if (contacts[i].firstname === prop) {
    return contacts[i].firstName;
}

Note the different casing for the firstName property.

Try for example the following snippet:

console.log(typeof {}.doesNotExist);

It will show up as undefined.

Last but not least: you would probably benefit more from learning how to debug than these answers :) Some simple debugging can be done by adding console.log calls anywhere in your code. Better even is to leverage debugging of your browser, or debugging using an IDE.

Upvotes: 1

Related Questions