Freecodecamp: Waypoint: Accessing Objects Properties with Variables

Created on 13 Jan 2016  Â·  31Comments  Â·  Source: freeCodeCamp/freeCodeCamp

This is not a bug, per se. The lesson for this waypoint seems very lacking.

Adding a specific example, in addition to the pseudo code example, would be very helpful.

learn

Most helpful comment

I'm not sure if this is the place to post this, but I was working on this Waypoint and I got it to accept my code as correct, but the console isn't showing the player's name like it's supposed to... just "" without showing the player's name ("Montana"). Thank you.

``````
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};

// Only change code below this line;

var playerNumber = 16; // Change this Line
var player = testObj[playerNumber]; // Change this Line```
``````

All 31 comments

That example is an actual example:

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

Is totally executable JavaScript: https://repl.it/Bd28

I do agree, in general, that this waypoint needs some love, though. Maybe more in prior waypoints or an additional waypoint or two to really explore the possibilities.

I agree. The code runs, but a _more specific_ example would be very helpful. It seems to me that after the Golf Code Checkpoint the lessons are less explicit and leave the student to struggle more by submitting trial code. Seems like the lessons are forcing "guess and check" or "brute force" efforts.

Maybe something like:

var dogs = {
"Fido": "Mutt",
"Rex": "Doberman",
"Snoopie": "Beagle"
};

var myDog="Rex";
var breed = dogs[myDog];
console.log(breed); // Doberman

"Rex": SaintPeter :smile: (That's my name)

That might work, actually. That does look clearer to me.

I'll flag this one as help wanted - Let's just call the dog something other than "Rex".

hehe...how about Murphy?

I'm not sure if this is the place to post this, but I was working on this Waypoint and I got it to accept my code as correct, but the console isn't showing the player's name like it's supposed to... just "" without showing the player's name ("Montana"). Thank you.

``````
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};

// Only change code below this line;

var playerNumber = 16; // Change this Line
var player = testObj[playerNumber]; // Change this Line```
``````

Yeah I just got this one finished and I saw the same thing Amy.
Also, I agree that a lot of these waypoints don't explain enough or even introduce new concepts without much explanation. Me and my friend both are struggling on the same ones trying to figure out something that is either explained poorly or is dropped on us without any background knowledge of how to use it.

@Tyrelx I feel the same way about struggling with the waypoints (I'm going back and forth between freecodecamp and a JS book just to make sure I'm really getting it.).

@SaintPeter @ken-murphy
I like the example you mentioned, but the essence of this challenge is in its explanation text quoted below:

Note that we do _not_ use quotes around the variable name when using it to access the property because we are using the _value_ of the variable, not the _name_

So I feel, the example should reflect that, to prevent confusion. Maybe use one like:

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

@amykotas I agree it should show up the player's name as it does in one of the previous challenges like this one here.

So, putting it together this challenge needs two things @SaintPeter

  1. Change the example. (edit) Add the example.
  2. Add the console output for the player name

1

I found it confusing because I thought I was supposed to use bracket notation to access the 16 key, when assigning the var playerNumber. As well as using bracket notation to define the var player. So I had the code half-right and was googling everywhere trying to figure out how to define playerNumber with bracket notation. Clearer instructions here would be appreciated.

@amykotas I use "Beginning JavaScript" by McPeak as a supporting material and it helps a lot. Highly recommend for novice coders.

I am glad I found this thread, I've been really worried I haven't quite been getting it all, so I re-read most of it to try and understand what I'm supposed to do. I am confident now I know others are having the same trouble.

what is wrong with the code above..

yeah acually its working

I've been just skipping the ones that I can't figure out what the exercise is asking me to do. Seems like the author is trying to be clever at the expense of clarity...Try always to remember how it is to be a noob!

Yeah it's seems like it continues... I passed it only because I looked up the code but honestly feel like I'm cheating my self as I don't understand exactly why there are two variables.

var playerNumber = 16;       // Change this Line
var player = testObj[playerNumber];   // Change this Line

Clearer instruction would be the best example to manage on how the program would flow.
thanks for the information on how to solve this.

Just wanted to agree with the above posters. The explanation for this and other recent examples is very, very poor.

Was stuck on this one aswell. Problem here is multiple "examples" are given and none of them is used in the exercise.

The one with the dog has 2 variables at the top, both get declared. The one with the Propname only has 1 var which gets declared. Suddenly we have to declare 2 vars when we are supplied with one.

It also feels illogical to use the notation required when you can just do var player = testObj[16] and it will output the same.

why the answer is
var playerNumber = 16;
why we can not use " var playerNumber = testObj [16]; " or " var playerNumber = testObj.16;
I don't get this.

yep this was unneccesary...

学习。

wow, I hate when I look up the solution for things. But dang do I feel dumb. It's just asking you to dig deep. So you make your var = 16. Which is a number, which is the same number in your object. Now lets use our var to look into the object. KK. Whyyyyyy not :)

For those who have a problem that "Montana" is not displayed - you should add a line :
console.log(player);
and it will be.

This is a really bad explained exercise. Thank you all for the help!

lol I was struglying with the same exercise, I dont really like to search for the answer, I prefer to do it myself but yes I agree that some exercises are not very well explained. sorry for bad english

earlier in the exercise it was explained that you can use bracket notation only were there is space in your variable but i cant see the connection now.

i feel that quite a few exercises are very poorly explained and even then you are required to do something you havent learned before. Atm im just trying to complete the course and i have very little understanding about it. After i finish it i think i need to go another round because i feel so dumb.

This is still badly explained. The second example is needlessly complicated. More complicated than the challenge itself.

Why use two variables in the example to access a single value? Just use one and stop over-complicating what isn't a difficult concept.

Glad to see that I wasn't the only one struggling with this exercise. I'm of the same opinion: the examples given don't reflect the final answer. Strange.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raisedadead picture raisedadead  Â·  3Comments

jurijuri picture jurijuri  Â·  3Comments

ar5had picture ar5had  Â·  3Comments

SaintPeter picture SaintPeter  Â·  3Comments

DaphnisM picture DaphnisM  Â·  3Comments