Use the Rest Operator with Function Parameters bug

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

Tell us what’s happening:
It looks like Javascript is okay with using the rest operator within an arrow function’s parameters section, as demonstrated by this returning an empty array:

console.log(
  ((...a) => (...b) => b)()()
)

With the following code, the challenge shows the “The sum function uses the ... spread operator on the args parameter.” requirement is not met.

marked as spoiler as I believe it should be a solution


const sum = (function() {
  "use strict";
  return (...args) => args.reduce((a, b) => a + b, 0);
})();
console.log(sum(1, 2, 3)); // 6

If taken literally, as this requirement having not been met, the following code should pass:

const sum = (function() {
  "use strict";
  const temp = (...args) => args.reduce((a, b) => a + b, 0);
  return (...args) => temp(...args);
})();
console.log(sum(1, 2, 3)); // 6

It fails on the same requirement.

The challenge cannot be passed with this solution structure, with or without the spread operator.

I believe the requirement is meant to read “The sum function does not use the ... spread operator on the args parameter”, as adjusting the first attempt to still not use the spread operator results in a pass:

const sum = (function() {
  "use strict";
  return function sum(...args) {
    return args.reduce((a, b) => a + b, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6

If I have misunderstood something about the way in which Javascript is parsed, or how this problem should be attempted, please let me know.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters/

the tests check exactly what you have written
to consider any possible way would be impossible, but if you think you can come up with tests that let more solutions that would be accettable pass, you can propose it on the github repository

I am extremely new to Javascript, and programming in general, so instead of throwing out ideas on GitHub and bothering people, I was wondering if I could ask you.

I believe this is the test runner?

It also looks like this project already uses babel, which looks like it includes a Javascript parser.

Would it be possible to make assertions on the content of the AST object returned?

Again, I’m new to programming, so please correct me. My thinking is that, no matter how convoluted the Javascript, if something a learner types in causes the Javascript parser to interpret and execute the semantics of a spread operator, the parser should be able to represent that somewhere in the AST.

Since the test runner is evaluating assertions, could an assertion be made against a walk of the babel-parser AST, looking for specific properties of the AST, like SpreadElement?

I have no idea, you can always raise the issue in the github repository

1 Like