john1
April 2, 2019, 1:31pm
#1
What am I doing wrong here? I have read the example, tested it and read more https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20%26%20beyond/ch2.md#spreadrest
and watched this:
And read other questions asked about this but I do not get it.
Your code so far
const sum = (function() {
"use strict";
return function sum(...args) {
const args = [ x, y, z ];
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum(1, 2, 3)); // 6
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.122 Safari/537.36 Vivaldi/2.3.1440.61
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters/
Can you explain why this is here:
const args = [ x, y, z ];
That’s your issue, and I think you aren’t quite understanding how this should work — that line of code doesn’t make sense.
john1
April 2, 2019, 1:37pm
#3
OK thanks for that, let me keep at it.
It wasn’t a rhetorical question! I can explain how to fix it, I just want to understand your thinking here because you’re the second person in the last day or so to write that exact same thing.
1 Like
john1
April 2, 2019, 1:41pm
#5
john1:
const arg
Right, I think I got it.
So I just removed the
const args = [ x, y, z ];
As the args are in the arguments in the function. The ...args
get all of fetches all of the arguments.
I think that is right, please correct me if wrong. This was helpful in understanding:
# You Don't Know JS: ES6 & Beyond
# Chapter 2: Syntax
If you've been writing JS for any length of time, odds are the syntax is pretty familiar to you. There are certainly many quirks, but overall it's a fairly reasonable and straightforward syntax that draws many similarities from other languages.
However, ES6 adds quite a few new syntactic forms that take some getting used to. In this chapter, we'll tour through them to find out what's in store.
**Tip:** At the time of this writing, some of the features discussed in this book have been implemented in various browsers (Firefox, Chrome, etc.), but some have only been partially implemented and many others have not been implemented at all. Your experience may be mixed trying these examples directly. If so, try them out with transpilers, as most of these features are covered by those tools. ES6Fiddle (http://www.es6fiddle.net/) is a great, easy-to-use playground for trying out ES6, as is the online REPL for the Babel transpiler (http://babeljs.io/repl/).
## Block-Scoped Declarations
You're probably aware that the fundamental unit of variable scoping in JavaScript has always been the `function`. If you needed to create a block of scope, the most prevalent way to do so other than a regular function declaration was the immediately invoked function expression (IIFE). For example:
```js
var a = 2;
(function IIFE(){
var a = 3;
console.log( a ); // 3
})();
This file has been truncated. show original
Cheers!
1 Like
That’s spot on!
I’ll have a watch of the video — as I say someone else asked the exact same question recently, and I’ve seen a few people over the last few months trip up on the challenge with the same mistake
1 Like
john1
April 2, 2019, 1:45pm
#7
Perfect,
Thanks for that, I’m not losing my mind then ha!!!
Cheers for the help - very nice indeed
1 Like