Freecodecamp: Challenge profile lookup - Can't pass a test case

Created on 12 Dec 2016  路  4Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge URL : https://www.freecodecamp.com/challenges/profile-lookup

//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(firstName, prop){
// Only change code below this line

  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      if (contacts[i].hasOwnProperty(prop)) {
        result = contacts[i];
      }
      else {
        result = i;
      }
    }
  }
    if (typeof result == "number") {
      return "No such property";
    }
    else if (typeof result == "undefined") {
      return "No such contact";
    }
    else {
      return result[prop];
    }
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Bob", "number");

Description:
lookUpProfile("Bob", "number") has returned "No such contact" but can't pass through the test.

Most helpful comment

This one was tricky to figure out. Your variable _result_ isn't declared in your function so it's scope is much larger than you intended, the solution is to just declare var result; at the beginning of your function so it 'resets'/starts as undefined when the function is called again for the next test case.

In it's current state _result_ is retaining the info as it has a global scope and passing it to the next test case... so when it tests bob and DOESNT set a value for result, it actually keep Harry's object as its value.

All 4 comments

For me, it's returning "No such property" and it should be "No such contact". So for now, I'm unable to reproduce and this seems to be a code error, but others please chime in.

This one was tricky to figure out. Your variable _result_ isn't declared in your function so it's scope is much larger than you intended, the solution is to just declare var result; at the beginning of your function so it 'resets'/starts as undefined when the function is called again for the next test case.

In it's current state _result_ is retaining the info as it has a global scope and passing it to the next test case... so when it tests bob and DOESNT set a value for result, it actually keep Harry's object as its value.

Closing as resolved?

Well Done Mate,
it looks me a while to figure it out . Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tzahile picture Tzahile  路  3Comments

ar5had picture ar5had  路  3Comments

robwelan picture robwelan  路  3Comments

kokushozero picture kokushozero  路  3Comments

danielonodje picture danielonodje  路  3Comments