What's the point in trying to help?!

Tell us what’s happening:
For the project below I figured it out but it took a while because IMO it is badly worded question. I followed the link “get help” and saw a few post were people were saying how to solve it (which I’d already done). I just wanted to add my 2 cents and provide some feedback by saying how the task should have been worded correctly. But because I’m a new forum user I don’t have permissions. So, how exactly am I meant to provide feedback??

As you can see I answered it correctly below but this bit I am on about this statement:

Use the playerNumber variable to look up player 16 in testObj using bracket notation. Then assign that name to the player variable.

There are two variables and two statement. It is clearly implied in the first statement (which refers to the first variable) that you have to somehow get the number 16 from the testobj object; it even says to use bracket notation to get it. Then once you have that, you tare then to assign it to the next variable. The statements are incorrect. The first task is as simple as assigning a number. The manipulation of the testobj object has to be done in the second variable therefore the statement should reflect this. In short, the bit about “bracket notation” in the first statement should not be there, it should be in the second statement. I figured it out in the end when I realized the statements were incorrect and you had to use the bracket notation method on the second variable NOT the first variable (which the guide says). I would recommend they fix this up to be clearer.

Your code so far


// 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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Accessing Object Properties with Variables

Link to the challenge:

In fact I’ve just confirmed another error in this guide that I suspected. I think the person that wrote it has no idea!

FYI I am new to freecode camp but I know a lot of Windows PowerShell so when I make my next statement I do actually know a little bit about objects.

In the testobj declaration it is actually a hash table and not even an object. I thought it looked strange when I did the test and I just assumed that JS objects are different to PS objects. So those that don’t know, a hash table is a collection of data types. So if you were to call the first object in that array you would see:

12 Namath

But an object has named properties like so:

Playernumber Name
12 Namath

Don’t take my word for it, look here in W3 schools, this is how I confirmed my suspicion https://www.w3schools.com/js/js_objects.asp

tldr from the link, this is how you define an object correctly

var car = {type:“Fiat”, model:“500”, color:“white”};

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like

I agree with your pointing out the wording of this challenge. Just went to redo the challenge and feel into that same trap…

should be more like:

Assign `16` in to the `playerNumber` variable.
Then use the `playerNumber` variable to look up `player` in `testObj` using bracket notation.

Could also use playerName instead of player.

I do disagree with the second part though:

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

is a valid Object in JS. The spaces/enters do not matter. And work well when making clean/readable code when you start chaining methods together.

the object in question has also named properties
the names are numbers, but it still has named properties

Honestly, I think it is pretty clear what is being asked for in this challenge.

My only real complaint about this challenge is the use of numbers and one of the requirements being that you have to assign a number to playerNumber. This hides the fact that properties are always of type String (or Symbol). The number assigned to playerNumber is going to be coerced to a string anyway.

Property Accessors: Property names

Property names are string or Symbol. Any other value, including a number, is coerced to a string.

let object = {}
object['1'] = 'value'
console.log(object[1])

This outputs ‘value’, since 1 is coerced into ‘1’.

You Don’t Know JS Yet: Objects & Classes

In objects, property names are always strings. If you use any other value besides a string (primitive) as the property, it will first be converted to a string. This even includes numbers, which are commonly used as array indexes, so be careful not to confuse the use of numbers between objects and arrays.

As for your second claim that it isn’t an object, all I can say is that you are wrong. It is very much an object, the syntax used in the challenge is also known as object literal. I think you are making conclusions based on incomplete knowledge about JavaScript. You are correct that JS objects are very much like an associative array, map, symbol table, dictionary, etc. names for key/value pair data structures. It is, however, an object never the less.

Speaking JavaScript Chapter 17. Objects and Inheritance

Roughly, all objects in JavaScript are maps (dictionaries) from strings to values. A (key, value) entry in an object is called a property. The key of a property is always a text string. The value of a property can be any JavaScript value, including a function. Methods are properties whose values are functions.

You may get the impression that objects are only maps from strings to values. But they are more than that: they are real general-purpose objects.

1 Like

@Iasorg, ok I see your point that it may be an object. You are misunderstanding me, I realize I may not
have been clear enough with my explanation: when I say it is not an object what I meant is that the description of what an object is (when you read literature and documentation) and how it should be used is not the case in this example. I am not disputing that JS sees this an object and the syntax is correct in order to create an object, I am saying that the author hasn’t created the object the way best practices say you should; this leads to confusion for students.To explain my point I’ll just describe how I know objects and hash tables work in PS and then maybe you can confirm if this is the same in JS.

A hash table is a collection of key/name pairs. This is exactly what the example looks like. Now I get that this maybe what JS refers to as an object (in other words from JS perspective hash tables and objects are the same) but there is still something wrong with the object. I’ll give an example - the code says this:

var testObj = {
12: “Namath”,
16: “Montana”,
19: “Unitas”
};

With objects they have properties right? So what this is saying is the the testObj object has three properties called 12, 16, and 19 and the values for those properties are the names. Does that look like a correct object to you? Properties are meant to describe what the value represents. How does a property named 12 give me any information about the value “Namath”? The way it should be is as so:

var testObj = {
name: “Namath”,
playernumber: “12”,
};

and then 2 more entries for the other two.

If you look at all other examples in the JS tutorial they all follow the pattern I just described so IMO it is clearly a mistake - the code creates an object but the author didn’t use the correct methodology.

To further prove my point, if I wanted to get the “name” property of the testobj object I would type it like so:

testobj.name

and it would show me the value of the name. But if you use the original data structure and wanted to see the first persons name you would have to type: testobj.12. See the issue here?

And some of your statements actually re-enforce my point:

Roughly, all objects in JavaScript are maps (dictionaries) from strings to values. A (key, value) entry in an object is called a property. The key of a property is always a text string. The value of a property can be any JavaScript value, including a function. Methods are properties whose values are functions.

Exactly! But the author is clearly trying to store a number data type in a property. The reason a property will always be a string is because it is meant to be a label of sorts, a short description to explain what the data value represents so therefore there is no need for any other datatype. This backs up what I’ve already said. Another example, if using the author’s data structure you would assume that doing a calculation an addition of the first and last number (12 and 19) we would get the sum 31 but as you already stated, they are stored as strings so we’d get 1219. But if these values were stored in the value part of the object they would be stored as numbers and the calculation would come out as expected. Can you see how confusing this would be for a beginner?? It’s just a bad example.

Back onto the two statements - you may call me pedantic or say I’m being picky but just cause you understood it first time it doesn’t mean it’s correct. The wording is clearly incorrect and when you are teaching it’s extremely important that the instructions are very clear. I only figured it out as quickly as I did because of my previous PS knowledge (I could see it wouldn’t work if I followed those instructions). For anyone else with no prior coding/scripting knowledge, if they’d have followed those instructions to the letter (which they should do) they would never reach the solution. You “thought outside the box” and worked it out, but my point is, we shouldn’t have to, it’s not meant to be a trick question.

You seem to have some wrong ideas about what a valid property name/key is in JavaScript and how an object should look and be used.

Speaking JavaScript: Unusual Property Keys

While you can’t use reserved words (such as var and function) as variable names, you can use them as property keys:

> var obj = { var: 'a', function: 'b' };
> obj.var
'a'
> obj.function
'b'

Numbers can be used as property keys in object literals, but they are interpreted as strings. The dot operator can only access properties whose keys are identifiers. Therefore, you need the bracket operator (shown in the following example) to access properties whose keys are numbers:

> var obj = { 0.7: 'abc' };
> Object.keys(obj)
[ '0.7' ]
> obj['0.7']
'abc'

Object literals also allow you to use arbitrary strings (that are neither identifiers nor numbers) as property keys, but you must quote them. Again, you need the bracket operator to access the property values:

> var obj = { 'not an identifier': 123 };
> Object.keys(obj)
[ 'not an identifier' ]
> obj['not an identifier']
123

Here is a blog post by Mathias Bynens worth reading as well Unquoted property names / object keys in JavaScript.

Your point about accessing a property on an object when it is a number is one of the reasons why the challenge is using numbers as property names. To teach about bracket notation.

const test = { 1: 'test' }
test.1
// Uncaught SyntaxError: Unexpected number
test[1]
// "test"

Here is a contrived example of having numbers as properties.

const dates = {
  1: 'January',
  2: 'February',
  3: 'March',
  4: 'April',
  5: 'May',
  6: 'June',
  7: 'July',
  8: 'August',
  9: 'September',
  10: 'October',
  11: 'November',
  12: 'December'
};

const month = new Date().getMonth() + 1;
console.log(dates[month]);
// January **or whatever the month is for you when reading this**

And just to make objects more confusing (not that it is my goal) you will learn that things you really don’t think are objects, in fact, are.

function imAnObjectToo(country, author) {
  console.log('Something is rotten in the state of', country, author)
}

imAnObjectToo.country = 'Denmark';
imAnObjectToo['written by'] = '- William Shakespeare';

imAnObjectToo(imAnObjectToo.country, imAnObjectToo['written by'])
// Something is rotten in the state of Denmark - William Shakespeare

Functions

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

One of the biggest issues people face when learning JavaScript is when they have come from other traditional class-based OOP languages. This is especially true when it comes to the Object model in JavaScript and prototypal inheritance.

As for the confusion about the challenge, I believe it is based more on a misunderstanding. If you carefully read the sentence I think it is fairly clear what it is asking for.

Use the playerNumber variable to look up player 16 in testObj using bracket notation. Then assign that name to the player variable.

The only way to use a variable to look up something inside an object is to assign to it, as a value, the given property name. As the variable is not a property on the object but contains a value we have to get to that value with bracket notation. So we can’t do this someObject.someVariable we have to do this someObject[someVariable].

const obj = {
  "Look me up": 'Find me'
}

const useMeForLookUp = 'Look me up'

obj[useMeForLookUp]
// Find me

We want the curriculum to be as good as it can be. It is an Open Source project and anyone can help improve it. I strongly suggest that you open an issue on GitHub with any improvements, corrections or ideas you have.

The same goes for the forum, we just want to help. I’m trying to help and teach the best I can.

Are you the author of that lesson? Just how you answered in your last sentence sounded like you might be.

No, I’m not the author. You can look at the commit history of that challenge to see all the different people that have helped and made commits to it.

I’m guessing based on your reply you think I’m being too defensive. That was not my intention, sorry if you feel that.

But you don’t really seem to care about what I wrote? Anyway, that’s fine, others may read it and learn something. Or someone may read it and correct me on some misconceptions I have. That is the great thing about this forum we can all learn together. It is also why freeCodeCamp being open source and mainly driven by volunteers is great, we can all help make it better.

All good. I do care about what you say, I guess I’m just a little bit biased as I’m coming from a PS perspective. I get it though that JS isn’t PS so things are different however, there are some things that universal right? And my understanding of objects I thought was universal. I respect your answer and I’ll read them soon enough but I just thought I raised valid point which you didn’t address like when I say about retrieving the object properties like so objtest.12. IMO that is an almost bullet proof example of why it is wrong. What are your thoughts on that? You didn’t answer either when i was saying that it would be confusing to beginners using that type of syntax to retrieve a value of a property.

Let me re-iterate - I am not questioning anything to do with the syntax, I am referring to how you are meant to create an object. The comparison always given when you learn about computer objects is that they are like objects in real life that have properties and then they give an example. In every case you will see a car or person or cube or anything and the properties will say engine, arms and sides respectively. When have you ever seen an example given where the property is named a number? The issue I have is the correct way to create an object should be shown. By all means, if you can create objects in different ways then show them (as in the lesson) but at least add a disclaimer first which states that it is non standard and objects should not be created this way as best practice.

I’ve read your links now and I can see where you are coming from and they are valid points. So really the lesson should just explain at the top that although objects can be created this way it is non standard. Again to prove my point I will pose this question to you: find me one article/doco on the net that is a lesson/tutorial about objects and that uses an example using numbers as the named properties and I will say I was wrong… but you won’t find one. The articles you have already linked to do show that you can use numbers as names for properties but that isn’t addressing the issue - just because you can do something it doesn’t always mean you should. You keep pointing out that the authors method creates a valid object in JS and showing me articles of how you can use numbers for this, but my whole point is not that you can do it but whether you should do it. There are clearly two thought processes of how you can create objects, that’s fine, but state that then in the lesson otherwise when you come from one lesson using one type and then go into the next lesson using a different type with no explanation why it confused people, especially noobs. Do see my point?

I did address your question about using numbers.

There are two ways of accessing properties on an object. “dot notation” (objName.propertyName) and “bracket notation” (objName[propertyName]). One is not more correct than the other, they both serve their purpose. However, there are rules for when you must use bracket notation. If the property name is not a legal identifier, such as when it is a number or a string with spaces, you have to use bracket notation. Or when using a variable (the value inside it) to access a property you must also use bracket notation.

For the challenge, I would say that using numbers for the property names serves as a way to teach the difference between dot notation and bracket notation. Because, as you know, you can not access the property using dot notation when it is a number. That is likely also why the challenge is using a variable for the object access because you can not access a property using a variable with dot notation. Both serve as a way to force the use of bracket notation and the challenge is teaching about bracket notation.

JavaScript objects are super versatile, they can work as simple key/value maps or full-fledged objects with methods and all. To better understand JavaScript it is worth looking into the history of it. However, that would take too long so I will just link to an old video interview with Brendan Eich which gives a bit of insight and I’d also suggest checking out the JavaScript: Understanding the Weird Parts video.

Not sure about tutorials but because you asked for something to show you that using numbers for object properties is in fact found in-the-wild here is an example from the React codebase. Look at the translateToKey object in the getEventKey.js file.

https://github.com/facebook/react/blob/master/packages/react-dom/src/events/getEventKey.js#L36

Now, do I think numbers are often used for property names on objects? No, I don’t. I think it is very specific to a particular need. But there is nothing inherently wrong with using numbers for property names. I would say that using them as explicit strings would be better than implicit. Which as I said is one of my gripes about the challenge.

Hopefully, I have answered some of your questions, or if nothing else linked you to enough resources that you can start to better understand some of the concepts.