[SOLVED] Profile Lookup - Could Need Some Guidance :-)

Hey,

I got stuck (again) with an exercise.

Your code so far


//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(name, prop){
// Only change code below this line
for (var i=0; i < contacts.length; i++){
  if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  }
  else if (contacts[i].firstName !== name){
    return "No such contact";
  }
  else if(!contacts[i].hasOwnProperty(prop)){
   "No such property";
  }
}
}
// Only change code above this line


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

I get following error message:

// running tests
"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
"Akira", "address" should return "No such property"
// tests completed

Am I missing something obvious?

ps. I am completely new to programming and find the problem solving part of codin more and more fun, but the steep learning curve between some exercises can be demotivating. I believe coding is similar to learning a language or math, where you master it by practicing practicing practicing. Hence, I am missing this repeatable practicing of concept here at FCC, any tips where I could get more practice of the same concepts?

When i is 0

  • first if statement, false because Akira is not Harry
  • second if statement, Akira is different from Harry so it is executed.
    The function returns "No such contact" and stop working and never check the other objects

Maybe you need to rethink of exactly where to put the various if statements

Is this because the return statement ends the function even if the loop has not finished yet?

Yes, the return statement when executed break from the function

Could you give me another hint? I am afraid I am hitting a wall here.

First thing, have you changed anything?
Tryed anything? Done any consideration with what I told you?

Yes, I played around and read more about the exercise.

function lookUpProfile(name, prop){
// Only change code below this line
for (var i=0; i < contacts.length; i++){
  
  if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  }
  
  else if (!contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }
}
    return "No such contact";
}

So, I put the return “No such contact” at the end, out of the loop. Now I receive one error message:

"Bob", "potato" should return "No such contact"

One thing that definitely confuses me was the challenge description:

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

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

It reads as if you have to create two additional else if statements, the second one returning no contact and the third one returning no property.

Your else if doesn’t check if the object match the name so it will check if it has a property on all contacts[i] and execute appropriately

Ok thanks @ilenia and @camperextraordinaire :pray:!

I finally managed it:

function lookUpProfile(name, prop){
// Only change code below this line
for (var i=0; i < contacts.length; i++){
  
  if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  }
  else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }
}
    return "No such contact";
}

Although I must confess that I am still uncertain of the exact why it works as it works.

Few things that confused me and I am still unsure about:

  1. In the description it reads as if I have to define three if (else if) statements. As beginner I read the instructions like an IKEA step-by-step manual. Now that I see the solution it makes obvious sense that “no contact” will be returned without an if statement (because it makes sense). What tactics / strategies can you recommend to tackle such exercises better conceptually?

  2. I am still confused when to use square brackets over dot notation. Any good resources?

  3. I didn’t know that I’d “kill” the loop by putting a return too early and therefore end the function. Any good resources?

Thanks again guys! I have been working on this problem 4-5 hours now. I want to get into the Hive School (If you heard about it) and started to learn coding on the site to train my problem solving mindset.

Before starting coding try to write the algorithm as pseudocode in the most detailed way possible

Practice practice practice

You can also find this useful:
http://pythontutor.com/javascript.html#mode=edit

1 Like

Thank you, both! :slight_smile: I will start to first write pseudocode that aims to depict the algorithm, before writing any real code. Really useful tip!