Am I right with my analysis?

Tell us what’s happening:
There are definitely 2 options one can code from here, one I chose was the “return” option because it’s doing something (returning), the other (x,y,z) is a variable initialized as an array, doesn’t do anything
I’ll admit I did struggle a little with this one but eventually came up with the above conclusion…and lo and behold…test passed :wink:
Is my logic correct?

Thank you)

const sum = (...args) => {
//const args = [x, y, z];
return args.reduce((a, b) => a + b, 0);
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Use the Rest Parameter with Function Parameters

Link to the challenge:

I’m not sure what you mean by “2 options”.

Adding in:

const args = [x, y, z];

shouldn’t do anything but break your code. You would be reassigning a parameter which is a no-no and I assume would throw an error. Also, since x, y, and z are not declared, I’m not sure what that line is trying to do anyway.

Can you elaborate the “2 options”. Can you fully write out the 2nd option?

1 Like

I don’t understand why you would say x,y and z aren’t declared? const…is the declaration?
option 1 the declaration (const args) with arrays
option 2 is the return of some arguments with a replace method (must still learn replace() :wink: )

hope this makes sense?

Not really.

const args = [x, y, z];

This line sets the variable args to hold an array with the values stored in the undefined variables x, y, and z.

Right, like Jeremy says.

Consider this code:

const x = 123;
const arr1 = [x];
console.log(arr1);
// [123]

const arr2 = [y];
// Uncaught ReferenceError: y is not defined at <anonymous>:1:15
console.log(arr2);
// [undefined] (if it compiled)
1 Like

First of all…why x,y and z are variables? aren’t they elements of an array? Secondly I don’t understand why x,y and z aren’t defined (they’re anyway elements of an array, no definitions needed really since they’re all array elements?)
(is how I understand arrays?)

They are on the right side of the equals sign, so you are saying to build an array out of x, y, and z.

A variable is not defined unless you define it.

To build on what Jeremy is saying, they are not recognizable by JS as values. This would:

const arr = [1, 2, 3];

because JS sees that those are numbers. This would work too:

const arr = ['x', 'y', 'z'];

because JS can see that those are strings.

Anything that JS can recognize as a value works just fine. But the way you’ve written it:

const arr = [x, y, z];

JS doesn’t know what x is. It’s not a number, not a string, not a boolean, not an object, etc. It also knows that it is not a keyword or some other part of syntax. It does see that it could be a valid identifier so it thinks, “Oh, the only thing left is that this must be a variable.” JS looks and sees that no variable by that name has been declared and throws an error because you clearly made a mistake because you are trying to reference a variable that doesn’t exist.

1 Like

But doesn’t const declare it a variable?

const declares the thing on the left side of the equals sign, not the thing on the right.

It’s like Jeremy was saying … In this statement:

const arr = [x, y, z];

The left half is declaring a variable:

const arr

The right half is an expression that will be evaluated and placed into that location of memory (the one create for and referenced by arr). The right half:

[x, y, z];

All of that has to be defined - either because it is an literal value or because it is a variable that has already been declared.


I imagine this dialogue:

Kevin: I have a new recipe for dip.

Jeremy: Really, what is it?

Kevin: The recipe is equal portions of sour cream, minced caramelized onions, and ingredient x.

Jeremy: What is “ingredient x”?

Kevin: It’s defined. const recipe = 'sourCream' + 'caramelizedOnion' + ingredientX

Jeremy: But that just assumes that that variable is defined somewhere else, but it isn’t.

There is no way to figure out what that is from the information given.

1 Like

see, I’m actually busy with this tutorial

the code you see is pretty much just my answer to that question, I just wanted to know if my analysis was correct and judging from the question it seems so because it asks for a sum function to “return” , from their code the only code that had a return was the one I used?

I do not know what you mean by ‘analysis’. You have said a lot of incorrect things so far.

This function signature

const sum = (...args) => {

cannot be used with this line

const args = [x, y, z];

because x, y, and z are not defined. This syntax is ‘backwards’.

However, that function signature is fine to use with

return args.reduce((a, b) => a + b, 0);

and this will give you the sum of the arguments passed to the function.

ok, first of all, yes, you just, what you just said with regards the return? It’s the only js keyword that corresponds with the question (it “returns”) so yes, you’d use that line of code for the answer. With regarding my “analysis”? It’s how I analysed the question to give the answer I mentioned. Does this make sense?
In other words…I used what you mentioned (using the return for output because that other line of code wouldn’t have given an output anyway…besides it being undeclared) to give the answer FCC was looking for but through my own analysis. I just wanted to make sure that was I was thinking (my analysis) was in fact correct, you just confirmed that (by using the return)

I really do not understand how you are using the word ‘analysis’. Like I said, you’ve said a lot of incorrect things, so I wouldn’t trust your ‘analysis’/understanding.

But, your current code (minus the commented out stuff) does pass the challenge.

Yeah, with all due respect, I think you are paralyzing yourself with analysis here. Just let it go for now. You’ve gotten a little closer to understanding. If you freeze yourself up until you understand everything perfectly, you will not progress. There were several things in my learning that I just had to say, “OK, I kinda understand this but I need to move on for now and come back to this later…”

1 Like

you’re right…yes, I’m a complete newb at js and lots of new stuff and yes, I passed the challenge. I think all good to know that the code I chose was right, it was a return so yeah, all good to let it go
Thank you)
(yes, I do have a bit more understanding, I’m in anycase going to go through everything before I start front end anyway…get a better understanding), but thanks for your help, I’m hoping you can help me a lot in future too because I like your explanations actually (no offense Jeremy, you do also try and I appreciate your efforts too;) ) but yeah, time to move on to more “difficult” stuff

And just remember that this is confusing stuff. Everyone struggles with something, and many people struggle with this. Just keep moving forward and getting better.

1 Like

Thank you SO much Kevin for the kind words…yeah sure ill keep moving…thats non-negotiable, its my future and my passion…so definitely no turning back, and like you said, it WILL get better, its just the teething im going through now so :wink:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.