Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Objects Properties with Variables Puzzling? #12141

Closed
ChrisKissoon opened this issue Dec 21, 2016 · 10 comments
Closed

Accessing Objects Properties with Variables Puzzling? #12141

ChrisKissoon opened this issue Dec 21, 2016 · 10 comments
Labels
help wanted Open for all. You do not need permission to work on these.

Comments

@ChrisKissoon
Copy link

ChrisKissoon commented Dec 21, 2016

Challenge Name

Accessing Objects Properties with Variables

Issue Description

This challenge confuses me. Since in previous challenges with accessing objects. I read that square brackets should be used only when there is a space in the property you need to access. And now in this challenge they want me to use bracket notation when the property does not have a space

@raisedadead
Copy link
Member

raisedadead commented Dec 21, 2016

/cc @FreeCodeCamp/moderators
Can anyone help in triaging if the instructions need some more clarification? I might be biased but they seem straight to me.

@raisedadead raisedadead added the status: discussing Under discussion threads. Closed as stale after 60 days of inactivity. label Dec 21, 2016
@Em-Ant
Copy link
Member

Em-Ant commented Dec 21, 2016

@ChrisKissoon Bracket notation is useful when you have to access a property whose name is collected dynamically during the program execution.
e.g.

var obj = {
  propName : "John"
}

function propPrefix(str) {
  var s = "prop";
  return s + str;
}

var someProp = propPrefix("Name")

console.log(obj[someProp]) // -> "John"

We could explain this concept in the description, if it's not too verbose

@raisedadead
Copy link
Member

Thanks @Emt-Ant, do we need to update the instructions for more clarification? Your opinion?

@IsaacAbrahamson
Copy link
Contributor

IsaacAbrahamson commented Dec 22, 2016

@raisedadead @Em-Ant,
I can understand the confusions, imho updating with more clarification sounds like a good idea.

For newer people with an example like this:

var someProp = "propName";
var myObj = {
  propName: "Some Value"
}
myObj[someProp]; // "Some Value"

it is easy to think, "Why not just do myObj.propName since we are doing the same thing"

Again, the second example:

var myDog = "Hunter";
var dogs = {
  Fido: "Mutt",
  Hunter: "Doberman",
  Snoopie: "Beagle"
}
var breed = dogs[myDog];
console.log(breed);// "Doberman"

does the same thing. It just attaches the value to a variable. Why do we even need a variable?? I definitely think that we should add like what @Em-Ant said, an example where the variable is changed dynamically. Because if you are just setting a variable equal to a string and using the variable, it does not make any sense. It would be more practical to just use dot notation. The challenge makes no sense unless you explain why we would ever need to use a variable in the first place instead of dot notation. So my vote is to add another example and clarify on why to use a variable.

@raisedadead
Copy link
Member

@IsaacAbrahamson thanks.

@raisedadead raisedadead added help wanted Open for all. You do not need permission to work on these. and removed status: discussing Under discussion threads. Closed as stale after 60 days of inactivity. labels Dec 28, 2016
@raisedadead
Copy link
Member

raisedadead commented Dec 28, 2016

Contributors need to refactor this challenge along with Updating Object Properties as well. Please comment below with suggested refactoring for both before making a pull request.

/cc @erictleung thoughts?

@raisedadead
Copy link
Member

Also please check @systimotic' #12125 (comment)

@valishah
Copy link

Hi @raisedadead ,
By reading all the issues related to object notations i understood it's not obvious for a beginner. They seemed straight to me. In order to make it even simpler how about making these changes.

Accessing Objects : (#12141)

var currency = {
   USA  : "dollar",
   London : "euro",
   India : "rupee",
}
function getCurrency(country){
 return country +" currency is "+currency[country] +" ";
}
getCurrency("USA") // "USA currency is dollar" 

I don't see any issue with Updating Objects . Correct me if i'm wrong.
Even i'm thinking to add Nested Objects challenge. Let me know how i can help to make this improve.

@erictleung
Copy link
Member

@raisedadead let's see if I can address this object issue.

Contributors need to refactor this challenge along with Updating Object Properties as well. Please comment below with suggested refactoring for both before making a pull request.

So for the initial challenge on objects, "Build JavaScript Objects", the sample object has all the properties being strings. Maybe we can add in another sample object with different typed properties, like

var sampleObject = {
  property_1: "Green",
  2: "Hal",
  "property_3": "Lantern"
};

This might be more confusing but it does explore the possibilities of objects, which may prompt campers to investigate a bit. Plus, the seed code for "Accessing Objects Properties with Variables" has numbers as properties, which hasn't been shown in the three previous challenges on objects.


I read that square brackets should be used only when there is a space in the property you need to access.

This could be fixed by changing how they are bracket notation is described in "Accessing Object Properties with Bracket Notation". The instructions

If the property of the object you are trying to access has a space in it, you will need to use bracket notation.

could be changed to something like

If the property of the object you are trying to access has a space in it, you will need to use bracket notation. However, you can still use bracket notation on properties without spaces.

And then we can modify the sample object to show you can also have properties without spaces.

var myObj = {
  "Space Name": "Kirk",
  "More Space": "Spock",
  "NoSpace": "USS Enterprise"
};
myObj["Space Name"]; // Kirk
myObj['More Space']; // Spock
myObj["NoSpace"];    // USS Enterprise

For "Accessing Object Properties with Variables", I like @Em-Ant's suggestion to replace, say, the first example, namely removing

var someProp = "propName";
var myObj = {
  propName: "Some Value"
}
myObj[someProp]; // "Some Value"

and moving the dogs object example as the first example and have something like

var obj = {
  propName : "John"
}

function propPrefix(str) {
  var s = "prop";
  return s + str;
}

var someProp = propPrefix("Name");
console.log(obj[someProp]); // "John"

as the second example.

@andreMc05
Copy link

How about saying that bracket notation is a more dynamic form of manipulating objects. You can use strings that can step outside of most of the JavaScript rules of declaration, like beginning with numbers and using string a with spaces as property names. You can also place variables inside of square brackets allowing properties to be assigned on the fly or via some side effect. Say for example you have an object in your program and you are not sure what the properties need to be during creation so you allow the program to assign them during run-time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Open for all. You do not need permission to work on these.
Projects
None yet
Development

No branches or pull requests

8 participants