Profile Lookup / Stuck

Profile Lookup / Stuck
0.0 0

#1

Tell us what’s happening:

I have tried this for quite a whine now and I don’t get anywhere. After first trying it with “if (name == contacts[i][“firstName”] && contacts[i].hasOwnProperty(prop))” (which didn’t work) I tried nesting the if/else statements.

What works are the parts that return “No such name” and “No such property”, so I tried every combination of bracket/dot-notation and quotation on the “if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];”-Part, but nothing worked. Now I think I am stuck, because to me, return contacts[i][prop] would at least have to do ““Kristian”, “lastName” should return “Vos”” correctly.

Please give me a hint how to do this! Thanks!

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup


#2

You can console.log( lookupProfile("Akira", "likes")and check your browser console: you will find that it works!

Why that one works and not the other ones? Think about that: Akira is the first guy in the contacts array and likes exists as a property.
Only if it is the first element works…it might have something to do with the loop! ( remember, once you hit a return statement you will be kicked out of the loop^^);

Good luck! :four_leaf_clover:


#3

I tried help but i get everything to work but contacts[0].prop comes back undefined.
there is probably somthing wrong somewhere. i cant return it like that but if i return
typed contacts[0].number it works.!


function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
    if (name == contacts[i]["firstName"]&& contacts[i].hasOwnProperty(prop)) {
        console.log(contacts[i].prop);
return contacts[i].lastName +' '+ contacts[i].prop;
}}
}
console.log(lookUpProfile("Sherlock",'number'));

#4

You’re misplacing your default return statement. Before I give you the answer, see if you can figure it out.

Sometimes formatting and block folding helps to find these type of errors. So head over here where I’ve already pasted your original code https://repl.it/repls/MetallicFrigidAdvance

If it’s not there, just paste it in yourself.

Then right click, and select ‘format document’.

Finally, hover over to the left hand side at the line numbers. If you see + or - by them, that means you can expand or close the block. This well help you narrow down where your function is returning.

If that doesn’t help you or I didn’t make it clear what to do, let me know.


#5

Thanks a lot, I think I am on the way to figuring out where I went wrong. even if I’m not there yet. I will try around for a bit and write again if it still keeps refusing to work properly. And thanks for the link, that website seems to me really useful for further coding-exercises.


#6

yeah i hope that is correct but do you know why
prop is not working?
like a wild card…


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

Let’s take a walk through the logic that results in ‘No such contact’.

First you want the result of lookUpProfile("Akira", "likes");

if (name == contacts[i]["firstName"]) {/*...*/}
else if (name != contacts[i]["firstName"]) {return 'No such contact'}

Since we’re in a loop, we will call this for every object

 contacts[0][ "Akira" ] === "Akira" 
/* true return contacts[0][ "Akira" ][ prop ] */

Now you want the result of lookUpProfile("Sherlock",'number')

 contacts[0][ "Akira" ] === "Sherlock" 
/* false return 'No such contact */

So what’s happening is that you are returning early when you don’t match the firstName prop.

A return statement inside a for loop will not only exit the loop, but the whole function. This is where your bug is hiding.


#8

Thanks to your post, I figured out a way to make the whole thing work, but I wonder if this really is the most elegant solution to do it. I now took the if-statement that returned “No such contact” out of the first for-loop and created for it a second for-loop, which worked in a way that all the required conditions to pass this challenge were met.
I just wonder if there is a way to maybe do this in a single loop or if/else-statement.


#9

Using these lines which you say you took out of the for-loop correctly,

else if (name != contacts[i]["firstName"]) {
        return "No such contact";
    } ;

now ask yourself this question:

After I loop through every contact and don’t match anyone, do I really need more logic? Or do I have all the information necessary to make a decision?

Always try to make your functions return as quickly as possible. The more code you write, the more places for bugs to hide. In this instance I think you’re still trying to verify something you already know.

Hint: If return exits the function early, then what happens if the for loop doesn’t match anyone?