freeCodeCamp / freeCodeCamp

freeCodeCamp.org's open-source codebase and curriculum. Learn to code for free.
http://contribute.freecodecamp.org/intro
BSD 3-Clause "New" or "Revised" License
401.12k stars 37.06k forks source link

Building a Todo App - Step 11 - implicit return #55851

Open Jake-winkler opened 4 weeks ago

Jake-winkler commented 4 weeks ago

Describe the Issue

The step doesn't call for an implicit return to be used, but it won't pass without using one. The following is what the step asked to do:

"Use const to declare a variable called dataArrIndex and assign it the value of taskData.findIndex(). For the findIndex() method, pass in an arrow function with item as the parameter.

Within the arrow function, check if the id property of item is strictly equal to the id property of currentTask."

Affected Page

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-localstorage-by-building-a-todo-app/step-11

Your code

The following code was my initial answer to this step.

  const dataArrIndex = taskData.findIndex((item) => {
    item.id === currentTask.id;
  })

This is the only code that I have been able to get to pass this step.

  const dataArrIndex = taskData.findIndex((item) => item.id === currentTask.id)

Expected behavior

This step should pass with my first code since the step doesn't call for Implicit return. The other option would be update the step to call for an implicit return.

Screenshots

No response

System

Additional context

Here is a link to my post out on the forum for this issue as well.

sanjeevmurmu commented 4 weeks ago

That's how javascript works when you define a function block you have to tell the interpreter what that block should return otherwise it will only execute those written within the curly braces and not return anything, without the curly braces the line after the arrow is treated as the return value

gikf commented 4 weeks ago

Please note the callback function needs to return result of item.id === currentTask.id, otherwise findIndex method will not work as intended.

Jake-winkler commented 4 weeks ago

Shouldn’t the step call for an implicit return then?

lasjorg commented 4 weeks ago

We should not require any specific syntax when teaching array methods. The code should pass with both implicit and explicit returns, it also shouldn't require an arrow function.

The explanation of the method API should include the callback. If you understand how it works, it should be obvious it must return a value. How the camper does it is irrelevant.

The findIndex() array method finds and returns the index of the first element in an array that meets the criteria specified by a provided testing callback function. If no such element is found, the method returns -1. The callback should return a truthy value to indicate a matching element has been found, and a falsy value otherwise.

The problem is, as per usual, we can't test the actual value, so we end up with a regex test. And testing for all valid code is a nuisance. It is, however, the correct thing to do here in my opinion. Requiring specific syntax when teaching a new method is doing a disservice to the camper. They should understand the API of the method so they can write the correct solution no matter what valid syntax they choose to use. That is how you know they understand the API, as exemplified by this issue.

aaronamoso commented 3 weeks ago

Shouldn’t the step call for an implicit return then?

No. The arrow function itself has its own syntax. That is why when using an arrow function, one of the perks is removing the return and when passing only one parameter, a curly brace isn't needed. For instance, const hello = ((item) => "Hello World!"); // hello = (item) => "Hello World!";

lasjorg commented 3 weeks ago

I know this was opened up to contribution but I don't think we have reached a consensus.

In my opinion, we should not force a specific syntax when teaching array methods and we should allow all valid code.

But, if we do force a specific syntax, we should not allow an explicit return. I see no reason to support an explicit return if we are not allowing all valid syntax (i.e. a normal function for the callback). In which case, we should force the use of the most concise syntax for the callback and the requirements should be updated to reflect this.


Allow all valid syntax:

  1. Update the description to include the callback.

The findIndex() array method finds and returns the index of the first element in an array that meets the criteria specified by a provided testing callback function. If no such element is found, the method returns -1. The callback should return a truthy value to indicate a matching element has been found, and a falsy value otherwise.

  1. Remove all mentions of arrow functions in the description, requirements, and test hints.

  2. Update the regex to allow for all valid code.


Allow only concise callback:

  1. Update the description to include the callback.

The findIndex() array method finds and returns the index of the first element in an array that meets the criteria specified by a provided testing callback function. If no such element is found, the method returns -1. The callback should return a truthy value to indicate a matching element has been found, and a falsy value otherwise.

  1. Update the requirements to specify that the callback must use an implicit return and not have a block body.

  2. Leave the tests as is, but update the hints to again restate the callback function requirements.

jeremylt commented 2 weeks ago

It might be worthwhile to request specific syntax specifically because learners don't consistently know what all the terms are at this point? Give them a chance to remember some technical terms.

lasjorg commented 2 weeks ago

True, requiring an implicit return could be seen as reinforcement.

My main concern with forcing specific syntax when teaching an array method is how it might affect the campers understanding of the API. For example, in this particular instance, the camper should understand that the callback must return a value, be it implicit or explicit. If the camper writes a callback that does not return a value, it points to us not teaching the API correctly.

In general, I'm OK with requiring an implicit return, but I fear doing so when teaching array methods might obscure the understanding of the API. But if the API requirements are properly included as part of the instructions, and then we implicitly ask for the concise syntax, that could work as well.