0

I am trying to solve a javascript exercise which gives me a fuction with two parameters(name, prop) AND an array with 4 objects, which each object has some properties, so i have to these tests:

  1. the function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.

  2. If both are true, then return the "value" of that property.

  3. If name does not correspond to any contacts then return "No such contact"

  4. If prop does not correspond to any valid properties of a contact found to match name then return "No such property"

    and I thought I had found two ways to answer a specific question, but one of them doesn't work and I really want to understand why.

The first code you're going to see works very well, and I already passed the test using this one, but the second one doesn't work and both seems almost equal, the only difference I can see is that in the first code I break the if into two pieces, which makes the second if only be achieved if the first if returns true

// First Code, this one works

function lookUpProfile(name, prop) {
  for (var 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";    
}

// Second Code, this one doesn't work

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

The error message says that the --return contacts[x].prop; -- isn't returning the right values only in the second code.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Eduardo
  • 407
  • 1
  • 4
  • 12
  • 1
    Would you please supply some test data? – Tibrogargan Aug 18 '19 at 04:54
  • 2
    It's not working because `return contacts[x].prop` is looking for a property literally called "prop", whereas `return contacts[x][prop]` is looking for a property with a name equal to the value stored in the `prop` variable – Tibrogargan Aug 18 '19 at 04:58
  • In first example, you wrote `return contacts[x][prop]`. In second example, you wrote `return contacts[x].prop`. – yqlim Aug 18 '19 at 04:59
  • "Bob", "number" -> this one works because there is no Bob in the array, and then it returns "No such contact" but if i try this one "Kristian", "lastName", it says it was supposed to return "Vos" there is the object which has Kristian as first name { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["JavaScript", "Gaming", "Foxes"] } – Eduardo Aug 18 '19 at 05:01
  • I just tried return contacts[x][prop], and it didn't work anyway – Eduardo Aug 18 '19 at 05:03
  • 1
    Don't edit your title. Either accept an answer, or post your own answer once you can, and accept that. Like all websites, Stackoverflow has a way of doing things and adding "solved" to your title is not part of that. Please take the [tour], and then read through the policy articles in the [help center](https://stackoverflow.com/help) – Mike 'Pomax' Kamermans Aug 18 '19 at 05:14
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Heretic Monkey Aug 18 '19 at 05:15

1 Answers1

0

This isn't really an answer, just showing that replacing return contacts[x].prop with return contacts[x][prop] works. You're doing something else wrong.

let contacts =
[ { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["JavaScript", "Gaming", "Foxes"] }
, { "firstName": "Bob", "lastName": "Bob", "number": "unknown", "likes": ["JavaScript", "Gaming", "Foxes"] }
]

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

console.log(lookUpProfile('Kristian', 'lastName'))

The issue really is that you're always returning on the first element in your array of contacts regardless to whether it matches or not. Try using a higher order function like find or filter to find elements that match your search term before refining the the property. Maybe something like this:

let contacts =
[ { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["JavaScript", "Gaming", "Foxes"] }
, { "firstName": "Bob", "lastName": "Bob", "number": "unknown", "likes": ["JavaScript", "Gaming", "Foxes"] }
]

function lookUpProfile(name, prop) {
  var contact = contacts.find( elem => elem.firstName == name )
  if (!contact) return "No such contact";
  if (contact.hasOwnProperty(prop)) return contact[prop]
  return "No such property";
}

console.log(lookUpProfile('Kristian', 'lastName'))
console.log(lookUpProfile('Bob', 'number'))
console.log(lookUpProfile('Bob', 'prop'))
Tibrogargan
  • 4,508
  • 3
  • 19
  • 38