Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove check for function length in more-or-less #2882

Closed
wants to merge 0 commits into from

Conversation

xySaad
Copy link
Contributor

@xySaad xySaad commented Feb 19, 2025

remove the test for add.length and sub.length because it's unnecessary.
example of code that wouldn't pass the test:

const more=(arg)=>arg+1
const less=(arg)=>arg-1
const add=(...args)=>args[0]+args[1]
const sub=(...args)=>args[0]-args[1]

@sadiqui
Copy link
Contributor

sadiqui commented Feb 20, 2025

@xySaad, despite that your code indeed adds the two first arguments, it isn't correct because it doesn't strictly enforce the requirement that add and sub must take exactly two arguments.

The subject states:

- `add` that takes 2 arguments and adds them together.
- `sub` that takes 2 arguments and subtracts the second argument from the first.

By using the rest parameter (...args), the function can accept any number of arguments, which contradicts the exercise's explicit instruction that it should take two. This means that calling add(1, 2, 3) or sub(5) (in your case) won't result in an error but will instead ignore extra arguments or produce NaN when arguments are missing. A correct implementation should explicitly define two parameters ((a, b) => a + b), ensuring that the function strictly adheres to the requirement.

@xySaad
Copy link
Contributor Author

xySaad commented Feb 20, 2025

@xySaad, despite that your code indeed adds the two first arguments, it isn't correct because it doesn't strictly enforce the requirement that add and sub must take exactly two arguments.

The subject states:

  • add that takes 2 arguments and adds them together.
  • sub that takes 2 arguments and subtracts the second argument from the first.

By using the rest parameter (...args), the function can accept any number of arguments, which contradicts the exercise's explicit instruction that it should take two. This means that calling add(1, 2, 3) or sub(5) (in your case) won't result in an error but will instead ignore extra arguments or produce NaN when arguments are missing. A correct implementation should explicitly define two parameters ((a, b) => a + b), ensuring that the function strictly adheres to the requirement.

@sadiqui, Even if the function takes only two arguments nothing is enforced, I can still pass any number of argument with no errors.
So it doesn't make sense to enforce the argument length in fucntion declaration.
As you know JavaScript follows permissive arity means i can pass extra arguments to the function with no error unless explicitly handled in the function, for example throw an error if arguments.length > Function.prototype.length

@sadiqui
Copy link
Contributor

sadiqui commented Feb 20, 2025

@xySaad, in a JavaScript exercise, following the instructions as given is key to good practice. The task explicitly asks for a function that "takes 2 arguments," and defining it as add(a, b) correctly reflects that requirement. While JavaScript allows extra arguments, using (...args) suggests unnecessary flexibility, making the function less clear. Good practice in exercises means writing code that matches the specification while maintaining readability and intent. Additionally, add(a, b) properly sets add.length to 2, preserving useful metadata, whereas (...args) obscures the intended arity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants