Freecodecamp: JS Iterate Through the Keys of an Object , challenge bug

Created on 18 Apr 2019  路  13Comments  路  Source: freeCodeCamp/freeCodeCamp


Describe your problem and how to reproduce it:
so the challenge ask the user to Iterate Through the Keys of an Object with a for...in Statement,
and return the number of users where online = true;
although i could pass the test without using a for in statement and just writing return 5; which is used in the assert test which is definitely not what we are expecting .

Add a Link to the page with the problem:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement

Tell us about your browser and operating system:

  • Browser Name: Mozilla
  • Browser Version: quantum
  • Operating System: windows 10

If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box):
image

learn discussing

All 13 comments

i think we should add a test on the user code and test if he used for in statement , i can work on this if you agree

This particular challenge has more than one issue. Behind the scenes, 3 additional users are added to the users object with an online property set to true. I think instead of adding extra users behind the scenes, the existing users object should either have one new user with online property set to true or change one of the existing users which has an online property of false to true. This way, the actual number of true values will be different than the number of false values for online in case someone accidentally writes the logic to get the online values of false. Then, the tests would need to look for the use of for x in users in the code using regex. Also, we would want to validate the user does not hard code the number of users with online = true in some way. This could be done, but would make the tests more complicated than necessary.

Honestly, I think the best way to approach this challenge is not rewrite it so that the challenge seed starts with the following:

function countOnline(obj) {
  // change code below this line

  // change code above this line
}

console.log(countOnline(users));

and the Instructions section shows an example of a users object that could be passed to the function. That way, we can generate the objects in the tests with various outcomes for the count of users with online property = true and actually show the users object being tests in the text of the tests. I am thinking there would only be 3-4 tests. The first would validate the campers's code uses the for x in z syntax. It will have to be flexible enough to handle a valid value for x and z, because the user could can use any valid variable name for x and the user could change the obj parameter to something else which is fine and can be handled via regex. The other tests should have objects with different number of users with online property set to true, so we don't have to worry about the user trying to hard-code the return values as much.

Before creating a PR, lets get some feedback from some other mods (@thecodingaviator, @Manish-Giri, @ojeytonwilliams).

@RandellDawson Your suggested changes sound good to me. Just to be sure though, I think you were proposing a challenge re-write here -

I think the best way to approach this challenge is not(?) rewrite it so that the challenge seed starts with the following

I also agree that we shouldn't be adding objects behind the scenes with online: true. Best to have a ready-to-use example in the challenge page itself that can be used directly.

Speaking of the tests, while we're re-writing the challenge anyway, how about checking to make sure something like .filter() isn't used in the code -

return Object.values(obj).filter(e => e.online).length;

Although if we're using regex to check for for..in, this might not be necessary.

@Manish-Giri I made some changes locally which could be used for a PR. Basically, the challenge instructions could look something like below:
image

The tests would look something like below:
image

I used an After Test div section to create the three userObjs used in the tests.
image

I think what should be done is just change the challenge seed to have the user just write the code and in the background, add the object containing users

@RandellDawson about the After Test Div did you add it in the same file i mean iterate-through-the-keys-of-an-object-with-a-for---in-statement.md ? if yeah can i go and create a PR with this solution ?

@sil3nthill I am not 100% sure if we are all in agreement yet.

@thecodingaviator i didnt understand what do you mean by " in the background, add the object containing users"

@sil3nthill If the other mods agree with what I have proposed above, then I already have a branch ready to create the PR.

@RandellDawson alright happy that i helped to spot the issue ! thank you @RandellDawson !

@thecodingaviator i didnt understand what do you mean by " in the background, add the object containing users"

@sil3nthill We'll add on object in the background i.e. the user would not see it being added. The object would contain the contents of the users object from the screenshot on the first post in this issue

@RandellDawson I like the proposed changes, too. So, please go ahead and submit the PR - it sounds like it's basically ready.

@ojeytonwilliams Done (PR #35899)

Was this page helpful?
0 / 5 - 0 ratings