Profile Lookup Testing Bug

Profile Lookup Testing Bug
0

#1

Tell us what’s happening:

This is not to ask for help but to report a bizarre testing error. As you can see I had to separate out the objects into for different arrays in order for all the tests to pass - otherwise, the for loop was unable to read the properties from the second object and moving on. Has anyone gone through this? I was not sure where to report this bug.

Your code so far


//Setup
var contacts = [
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    },
];

var contacts2 = [
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
]

var contacts3 = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
]

var contacts4 = [
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
]


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 {
  //     return 'No such contact';
  //   }
  // }

  if (name === contacts[0]['firstName']) {
      if (contacts[0].hasOwnProperty(prop)) {
        return contacts[0][prop];
      } else {
        return 'No such property';
      }
  } else if (name === contacts2[0]['firstName']) {
      if (contacts2[0].hasOwnProperty(prop)) {
        return contacts2[0][prop];
      } else {
        return 'No such property';
      }
  } else if (name === contacts3[0]['firstName']) {
      if (contacts3[0].hasOwnProperty(prop)) {
        return contacts3[0][prop];
      } else {
        return 'No such property';
      }
  } else if (name === contacts4[0]['firstName']) {
      if (contacts4[0].hasOwnProperty(prop)) {
        return contacts4[0][prop];
      } else {
        return 'No such property';
      }
  } else {
      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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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


#2

You can not change the original contacts array. The tests expect it to be the same. The bug was in your original code (I assume the commented out part shown in your post?). If the commented out section was what you were using in your original code, then you were exiting the function within the first iteration of the for loop? Why? Because when a return statement is executed, the function exits and does not return to complete a loop.

When you see comments in the code like below, there is a reason why they exist. They do not want you changing any code outside of them.

// Only change code below this line

// Only change code above this line

#3

Thank you Randell! That fixed it :slight_smile: