I took a little different approach to solving this challenge. I had tried several other approaches over the course of an afternoon but none of them were as promising as the one I am currently pursuing (I hope it doesn’t make your eyes hurt too bad). I’ve narrowed down the source of the problem to line 43 in onecompiler (the return statement that is not executing). I’ve spent the last several hours trying to discover why this return statement is not executing and I spent almost 2 hours on irc #programming last night - they kept giving me ALL these hints but I’m just not grasping the reason for the failure. I feel as thought I could spend the rest of my life guessing and never figure it out.
Yes, I would like to explore other approaches BUT not before solving the problem using THIS approach I’m on…
You can see the code (with debugging lines) in code in oencompiler ← this reveals A LOT about what is actually happening in that code and just how close it seems to be to working.
Here is my code so far without the debugging lines…
// Setup
const 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
if ( typeof lookUpProfile.i == 'undefined' ) {
lookUpProfile.i = 0;
}
if (name == contacts[lookUpProfile.i].firstName && contacts[lookUpProfile.i].hasOwnProperty(prop)) {
return contacts[lookUpProfile.i][prop]; // THIS IS THE LINE THAT IS SHOWING UP AS "undefined" --> IT IS SOMEHOW NOT GETTING THE RPOPERTY AND RETURNING IT --> IDK WHAT THE PROBLEM IS OR HOW TO FIX IT
} else if (lookUpProfile.i < contacts.length) {
++lookUpProfile.i;
lookUpProfile(name, prop);
}
// Only change code above this line
}
console.log(lookUpProfile("Sherlock", "likes"));
// console.log(lookUpProfile("Kristian", "likes"));
Line 37 (here / above) seems to be the problem but why? And how to fix it?
Note that when function is called with “Akira” and “likes”
console.log(lookUpProfile(“Akira”, “likes”));
When it is called as shown above it returns [“Pizza”, “Coding”, “Brownie Points”] just fine and it
has to execute the same block of code - specifically - line 37 (line 43 in onecompiler) in order to do so.To sum it all up…
if the function is called with the name corresponding to the name in the first object it works fine.
If it is called with any name corresponding to a name in subsequent objects it fails with ‘undefined’
I realize that there are additional requirements that are not being addressed in this code but I want to get this part working first and then add the solutions for the additional requirements to the code after that.
Thanks for any help
// Setup
const 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
if ( typeof lookUpProfile.i == ‘undefined’ ) {
lookUpProfile.i = 0;
}
if (name == contacts[lookUpProfile.i].firstName && contacts[lookUpProfile.i].hasOwnProperty(prop)) {
return contacts[lookUpProfile.i][prop]; // THIS IS THE LINE THAT IS SHOWING UP AS “undefined” → IT IS SOMEHOW NOT GETTING THE RPOPERTY AND RETURNING IT → IDK WHAT TO DO TO FIX IT
} else if (lookUpProfile.i < contacts.length) {
++lookUpProfile.i;
lookUpProfile(name, prop);
}
// Only change code above this line
}
lookUpProfile(“Akira”, “likes”);
**Your browser information:**
User Agent is: <code>Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0</code>
**Challenge:** Profile Lookup
**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup