Why is my answer for 'Profile Lookup' not passing if it is returning everything correctly?

I have console.log() at every point immediately before the return, and they all give the correct results.

I know it is janky, I was wanting to use recursion instead of a for loop because I had never used recursion before and wanted to do it for myself. Is a for loop the only solution accepted?

// Setup
const contacts = [
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
  firstName: "Harry",
  lastName: "Potter",
  number: "09994372684",
  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
var n = contacts.length -1;
function recur(contacts, n, name, prop) {
  if (n < 0) {
    console.log("No such contact");
    return "No such contact";
  } else {
    if (contacts[n].firstName == name) {
      if (contacts[n][prop] != undefined) {
        return contacts[n][prop];
      } else {
        console.log("No such property");
        return "No such property";
    } else {
      recur(contacts, n-1, name, prop);
recur(contacts, n, name, prop)
// Only change code above this line

//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/101.0.4951.67 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Side note: This is not a great problem for recursion, and faux/inner recursion is not a great version of recursion.

Hey there! Welcome to the forums :wave: :slightly_smiling_face:

Try using console.log() on your final function call, lookUpProfile("Akira", "likes");

Looks like you’re getting an undefined, hmm. I’d look through your codes flow again, there are a couple places still missing a return.

Other than that, well done. I applaud you for pushing yourself past the lessons boundary. Of course I am gonna say that recursion is rather resource heavy given the task, and in production code I would just opt for the for loop in this case. Still though, I’m one to try solutions other than the “best practice” if I’m just fiddling.

Only other thing (I see) is that, as was said above, you could improve effeciency a bit by not using a nested function.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.