Freecodecamp: Profile Lookup confuses with same param name and attribute name

Created on 4 Jan 2017  路  7Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Profile Lookup has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:


//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(prop1, prop2){
// Only change code below this line
  var i = 0;
  while(i != contacts.length) {
    if(contacts[i].firstName == prop1){
      if(contacts[i][prop2]){
        return contacts[i][prop2];
      }else{
        return "No such property";
      }
    }
    i++;
  }
  return "No such contact";
// Only change code above this line
}

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

In the Profile Lookup challenge code, the lookUpProfile function's first parameter name is "firstName" which is the same as the array objects "firstName" properties.

The problem is that a novice learner like myself can pass the test without even realizing that the lookUpProfile function's first parameter "firstName" is not at all the same thing as the array objects "firstName" properties. It's really easy to assume they are one and the same.

I propose we change the lookUpProfile function's parameter "firstName" to something like "prop1" (for example like I have it posted here in the code above). Honestly, while I was solving this challenge it was very confusing at first until I realized that "firstName" was used for two different things (function parameter and the array objects properties) in the code.

Any thoughts or other suggestions.

first timers only help wanted

Most helpful comment

Hi, just sent a pull request with the fix for this

All 7 comments

Hello!
Well, your argumentation is good, but i think that it's good to learn how the language works. It's like scopes.
Another point is about logic, you want find "firstName" then you'll find it in "firstName" propety, this make sense. It's just how i think.

I considered your points of argument before I posted this. However, I don't agree that this is a good way to teach a student about scopes since it was never clarified in the instructions. It just creates confusion and/or wrong assumptions that "firstName" is the same thing throughout the code. And, student can actually pass the challenge without even realizing there's a difference. The student is not learning about scopes in that case but left to assume that there is no boundary in the scopes, and that somehow its all the same. Clarity in stead of trickery/obscurity is a much better way to teach something in my opinion.

From what I understand, it goes against best practices to share the same name. In which case it's actually teaching the student that it is ok or even good to write code that way, provided they even realize there is a difference between the two "firstName" elements.

  • Don't reuse same variable name in the same class in different contexts: e.g. in method, constructor, class. So you can provide more simplicity for understandability and maintainability.
  • Don't use same variable for different purposes in a method, conditional etc. Create a new and different named variable instead. This is also important for maintainability and readability.
  • https://dzone.com/articles/best-practices-variable-and

What about changing the lookUpProfile function's parameter name to "name1st" instead of "firstName"?

@spoonhonda: Thanks Dave for bringing this up.

While we are strictly aiming just to teach basics, and best-practices are beyond scope of interactive coding challenges (they are best taken in the video tutorials), this one can be considered a corner case IMHO.

@FreeCodeCamp/moderators It looks like a favorable change to avoid confusing campers. Please advice and tag help wanted, if you agree.

And while we are at it lets just name it something meaningful to address the scope and point brought up by @joaofreires

It's like scopes. Another point is about logic, you want find "firstName" then you'll find it in "firstName" propety, this make sense

Happy Coding!

@raisedadead
I think changing this could take away confusion for some people, and I don't think changing it will confuse anybody, so I think this change is worth it.
Would name be a good balance between meaningful and different?

Agreed @systimotic

First-timers only

Check out CONTRIBUTING.md to get your local development environment set up.

The seed for this challenge is here. Change the firstName parameter to name.

If you need help, please go to our contributors chatroom.

Happy coding! 馃帀

Hi, just sent a pull request with the fix for this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tzahile picture Tzahile  路  3Comments

imhuyqn picture imhuyqn  路  3Comments

kokushozero picture kokushozero  路  3Comments

trashtalka3000 picture trashtalka3000  路  3Comments

bagrounds picture bagrounds  路  3Comments