freeCodeCamp: Possibe error in code example

Describe the Issue

The example shows “const thirdLetter = alpha[2]”. Below that, the snippet of text says that alpha[2] should be “Y”. “thirdLetter is the string Y, lastLetter is the string C, and valueLookup is the string Y.”

Affected Page

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups


const alpha = {
  1:"Z",
  2:"Y",				//<--------------- thirdLetter ??
  3:"X",
  4:"W",
  ...
  24:"C",				//<--------------- lastLetter ??
  25:"B",
  26:"A"
};

const thirdLetter = alpha[2];
const lastLetter = alpha[24];

const value = 2;
const valueLookup = alpha[value];

The snippet has the below explanation in the instructions:


`thirdLetter` is the string `Y`, lastLetter is the string `C`, and `valueLookup` is the string `Y`.

Expected behavior

I believe correcting the const “thirdLetter” to “secondLetter” could be less confusing.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (14 by maintainers)

Most upvoted comments

I like that example better. I think we should use it + the corresponding text fixes that would be needed.

Yeah, the numerical index is odd.

Does it have to be an alphabet? Is there a better, simpler example?

I can’t think of a better example to be honest, but we can use a practical one. Like how the freeCodeCamp codebase Json files structured.

const article = {
  "title": "How to create objects in JavaScript",
  "link": "https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-in-javascript-b0e2450655e8/",
  "author": "Kaashan Hussain",
  "language": "JavaScripts",
  "tags": "TECHNOLOGY",
  "createdAt": "NOVEMBER 28, 2018"
};

const articleAuthor = article[author];
const articleLink = article[link];

const value = "title";
const valueLookup = article[value];

The variable name in current example is plain wrong. Nobody said that was not the case.

Nothing is forcing us to use 1 based indexing here. Personally, in addition to fixing the variable name, I would use 0 based indexing.

However, I’m thinking this object should be replaced with a simpler object. That would increase clarity and remove the strange indexing.

We’re discussing how to fix the problem. Everyone seems to agree that the problem exists.

Yes, we need to wait. The issue isn’t ready for contributions when discussion is ongoing.

Yes, I understand how objects work. I don’t see a reason to start counting at 1 here when in general JS starts counting at 0.