Spreadshet - Step 103. Why does site use spread operator in answer?

Here’s the step:

Add a range property which generates a range from the first number in nums to the second number in nums. Remember that you have a range function you can reuse here.

And my answer for it was this, which was accepted: range: nums => range(nums[0],nums[1])

But when I go to the next step, the website changes it to this, which makes no sense to me: range: nums => range(...nums)

We were told about the spread operator earlier: “The spread operator (...) allows you to copy all elements from one array into another”

So why would the spread operator work in this situation? Our array can be longer than 2 elements. Thanks for your help!

js has an interesting feature. If you give a function in js more parameters than it wants, it just ignores them.

In the case of the rest operator, it is passing in every value in the array nums. If that is 2 values, then the function will get 2 values, if that is 3, the function will still only see 2.

edit: and here’s a reference to spread. It essentially splits up the array into its individual parts when it is used the way you saw in the step

1 Like

Okay, I understand, thanks!

Do you mean spread operator? Is “rest operator” another name for it?

Yes sorry. I often mismatch the two names.

1 Like