Use the Rest Operator with Function Parameters - Random error?

I have no idea why i cant past the challenge Use the rest operator with the error:

“The sum function uses [should use] the … spread operator on the args parameter.”

const sum = (...args) => {
Did i do this wrong?

–> https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions
–> https://repl.it/@John_Nicole/ES6-10-Use-the-rest-operator
(Press fork at top)

const sum = (...args) => {
return args.length > 0 ? args.reduce((num, i) => num+=i) : 0
// num is accumulator, i is the number.
}
console.log(sum(2,3,4)); 
// X, The sum function uses [should use]  the ... spread operator on the args parameter.

Two things:

  1. arrow functions are anonymous functions
  2. you must return a named function from the main function in order to call it.
const sum = (function () {
  "use strict";
// the outside world doesn't know this anonymous function
// exists because you didn't return it
    const sum = (...args) => { 
      return args.length > 0 ? args.reduce((num, i) => num += i) : 0
    }
})();
console.log(sum(1, 2, 3)); // 6

That makes no sense. It gets the job done (works) , its simple, you can call it, it only uses one function. There is no point of putting those two functions inside of each other.

You really need two functions for 1 line of meaningful code?

It has a name, its not anonymous.

Yeah, when i call sum() with numbers i get there sum. It returns what i need. Don’t see any other way it should be done accept the simplest, and most readable way possible.

I did the exact same, 1 function thing on the previous challenges and it worked. I didn’t do functions inside of functions like the examples were.

Are you trying to substitute your sum function for the increment function in the challenge?

They are testing your understanding of closures. The sum function you are calling is the returned one, not the main one. I know it doesn’t say so, but that’s why they pre-write the structure of the function.

If you want to change their boilerplate, you’ll still need to create an immediately invoked function (IIFE) to create a closure around the function you want returned.

const sum = (function() { // < IIFE, we are not calling this
  "use strict";
  return function sum(x, y, z) { // we are calling this closure
    const args = [ x, y, z ];
    return args.reduce((a, b) => a + b, 0);
  };
})();

Incorrect. The variable you declare has a name. Arrow functions are expressions so the function itself is anonymous because only function declarations can be named.

These function expressions are best suited for non-method functions, and they cannot be used as constructors.
Arrow function expressions - JavaScript | MDN

Function Expressions function expression - JavaScript | MDN

Arrow functions are anonymous and change the way this binds in functions.
Arrow Functions in JavaScript: Fat & Concise Syntax — SitePoint

Here are the only 4 ways to define functions

// Function declaration
function someFunc(){}

// Named expression
const someFunc = function someFunc(){}

// Anonymous expression
const someFunc = function (){}

// Fat arrow expression
const someFunc = () => {}

// Not valid syntax
const someFunc = someFunc() => {}

And this is why arrow functions are inherently anonymous.

1 Like

If I’m not mistaken you have links to a different FCC challenge that the one you are asking about.

I meant i did the same format @alhazen1

Why is it forcing me to do one?

Coming from ES5, i just see it was making a readable function, that works and gets the job done. I don’t get the point of anonymous functions, i just see my way as a simpler way of writing functions (Fat arrow expression).

I have no idea since I don’t work for them. My first assumption is that since this site is geared towards getting you acquainted with real world type code, and IIFEs are a mainstay of javascript apps, they attempt to get your eyes used to seeing that syntax.

Since contrary to popular belief, programmers spend more time reading than coding, I think it’s good on them for forcing us to see different structures.

Then someone like you comes along and questions why, which hopefully sends you into learning more about design patterns.

But you didn’t account for the structure they provided, which is using a specific design pattern.

Whether or not you agree with their initial data structure, you have to do it their way.

In the real world you will be handed specs you must adhere to. And yes, that can include limitations on your coding style.

So do I. The only issue is you are working outside the spec

So teaching me a bad structure that is not as readable is good?

Its easier to read code if it is simple.

Ok, now you’re just being facetious. How can it be a bad structure if it’s found all over the javascript world. Or do you plan on rewriting the entire javascript library ecosystem using your own patterns?

It’s perfectly readable. It’s a very common design pattern. The fact you find it less readable means you’re not familiar enough with this language.

You obviously didn’t read the link before, so here

Easier for who? You. That’s it. No one else finds this problem particularly hard. At least not by your measure. Besides, you could still use arrow functions inside the IIFE if you wanted like so:

const sum = (function () {
  "use strict";
  const sum = (...args) => {
    return args.length > 0 ? args.reduce((num, i) => num += i) : 0
  }
  return sum
})();

Which you didn’t even try because you didn’t know what the code was doing to begin with. Which is really where you should start. A dev spends more time reading code than writing.

Now this structure returns the correct results, but won’t pass the final test. Why?

Because using it refuses to follow the spec.

This kind of thinking is going to bite you hard in the real world. In the real world we code to spec. Not to personal preference.

For instance, I love functional programming and using monads. But if someone needed me to use OOP patterns, I’d be able to accomplish the same task. Good luck keeping the job if I can’t.

Complaining that I find functional programming easier is futile. We don’t code just for ourselves, but for everyone else that will need to troubleshoot our bugs later. This is why standards and coding policies are put in place.

Best to start getting used to this reality.

1 Like

No its easier for everyone since Its basically a 1 line function

Same here.

Tell me. How is it good? You can almost link this to the arrogance in coding. People don’t want a simpler way to do things because more code makes them look cooler right? I thought ES6 was about smaller more powerful code, not putting functions inside of functions for 1 line of code since logic.

Are people this picky about functions in coding that you can’t have it be 1 function because its ‘tradition’ not too? IT ALL ACCOMPLISHES THE SAME THING. THERE IS NO DIFFERENCE BETWEEN THE TWO, YET ONE IS SIMPLER (MINE).

Keep linking me to articles, but ill just be right.

Yes you’re right. I forgot that this specific code is real world :roll_eyes:. Stop being silly. You’re here to practice standards.

Guess what, sometimes you have to practice on silly little toy problems if you can’t read anything more complex. And judging by your responses, you’re in no position to determine “what’s needed in production code”.

Again, you’re either trolling or straight simply don’t understand that FCC tests are not production level code.

Here you are complaining about a simple revealing module pattern, because you can do it easier for this made up test. YET, you have no idea what it even is.

So please educate me here…how is it that you know this is a useless, complicated pattern, when you couldn’t even tell what it was when you read it?

Is the standard of what’s easy determined by what you can read?

And no, you couldn’t read it because you didn’t even recognize the data structure when you saw it. It may as well have been dirt smeared on your screen. Seriously dude…

You wrote a one-liner. Yes, you’re now ready to architect an app :clap:

No it does not. Unless you discovered a way to encapsulate private data inside an arrow function, which doesn’t even have it’s own execution context, and return a module.

Yes. I can see your future coworkers, if you ever get to be so lucky, will just have a great time arguing with the newbie about what’s simpler. Lulz.

“Hey guys! I’m serious! You don’t need a module pattern! On FCC I just used arrow functions everywhere!”

smdh…

But what do I know? I only started learning to code last year in January, and got to level 3 kyu in codewars by december because FCC made things more difficult than it needed to be.

It definitely wasn’t because I researched all the data structures and patterns they introduced me to. Nah…it couldn’t have been that.

Feel free to have a look :slight_smile: JM-Mendez | Codewars

And if you don’t mind, would you share your solution to this simple, level 5 kata?

I’d very much like to learn how you’d code this simply, just using one line arrow functions. And not any other well known design pattern.

And this is level 5. Just a little above entry level…ah, who am I kidding? You don’t need me to explain. I’ll just let you get to proving me wrong.

Want to know something?

The challenge asks you to sum all the input numbers, using the rest operator in which i did. I used ES6, and its as simple that.

I don’t have to tell you what that code is doing. All i know is i was given a problem with the fact that i need to use rest operator, i did it, and it works. I call a sum() function and expect a sum, in which you get, no need for anything extra.

You know, previous ES6 challenges have presented code like this:

var magic = function() {
  "use strict";
  return new Date();
};

Which is the same as:

var magic = new Date()

And the requirements only tell me to make it a arrow function, and change the var to const. It doesn’t even point out the fact that there is no need for any of that, because it can be easily simplified. Not the same format, but its simple and works.

Fast forward to here. I saw a challenge and i made simple code that returns the sum of all your input numbers. I use ES6 features including the requested rest operator. There is no need for anything else besides that.

This shows FCC challenge answers can be way to confusing or vary too much for different skill levels and backgrounds. I want to write simple clean code, and when i am learning ES6 i am expecting to do that.

But…

Exactly. We’re back to coding to the spec. And you don’t decide the spec unless you’re in charge.

And you learned a valuable lesson here — always read the spec

Did you also tell your teachers in school what and how to teach you? I’m sure you were at least able to make suggestions?

Please tell me…how does someone with no experience decide what is needed to gain experience?

I’m not even gonna be nasty on this one…

  • The first one means the complexity of the code in the tests are not production level.

  • The second is not even speaking about the level of code. It’s addressing the fact we code to specifications.

So you’ll have to either explain what you’re confused on.

Its a online free website course that is student paced. ???

Are you seriously resorting to this kinda of response ignoring my proof? This is a new curriculum, am i not allowed to say if their is anything wrong WITH PROOF?

You are also not showing the fact that these are just challenges, and in your logic if answers vary it can be compared to ‘telling a teacher how to teach you.’

School is required by law, FCC isn’t. I am here because i want to be, so ill critics if a update is not in my ideas correct.

Yeah i did, i followed the directions and returned the simplest answer. I used the rest operator, and i used 1 function. Its the simplest answer, and telling me i’m wrong for not following a specific format that is not needed is just wrong.

No need to code to specification, since their is really is none. Do you think a employer really cares to go back and edit all of your already simple readable working code just to add unnecessary functions for ‘tradition’? No, because in all reality they really don’t care. They care you get the job done in the simplest way possible.

Starting a new thread to better see everyone’s side.

–> ES6 formatting, what am i doing wrong?

Better collects my ideas.

You won’t accept the explanation given to you. Instead you resorted to saying things like

Not when there are test cases you need to pass. Which is called coding to the spec. A lesson that every aspiring dev needs to learn.

Except you didn’t follow the directions because

Following the format is part of the test. So I’m confused with where you’re going with this.

Except there is. They’re called test cases. The cases tell you what the required output is. If you don’t match those specifications, you fail the test.

They care you get the job done to spec. You still insist on claiming that an employer won’t care how you code. Which is completely, and utterly false.

Most devs work in teams. And what one person finds easy, another finds hard. So each team sets up their own coding standards so they’re all on the same page.

Don’t believe me it’s that serious? Just look at two of the biggest style guides. They explicitly control how you write your code.

And this is just for styling. Imagine how much more strict the architectural requirements will be.

and

https://google.github.io/styleguide/jsguide.html

1 Like

It’s fine. I’m only trying to tell you how it is in the real world. We don’t have to keep going back and forth. Best wishes though.

I suggest reading my post, ideas are more connected and make best of sense.

You only care about and only see the output of a function, so the simplest, readable way to do it is the best way regardless of format.

It takes a lot to be a programmer, don’t see how its going to be hard to read my code considering i feel as if its less w/ same output.

I did read it. But at this point I think it’s best if I let that one live on it’s own.

I agree with you. And I also agree with your code. If I was an employer your code would pass. But the purpose of this site it to get you acquainted with programming concepts.

If all you do is FCC you’re going to lag behind. And part of the real world is coding to specs, which are sometimes written as tests.

It’s frustrating, but that’s part of the process really. So is getting incomplete instructions, half misleading instructions, etc. Best to get used to it here is all that I was suggesting before we let the thread spiral out of control.