Assignment with the Rest Operator to Reassign Array Elements

Tell us what’s happening:
hello, my attempt to an a correct answer was completely different and yet it passed. Is this ok?

Your code so far
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
“use strict”;
// change code below this line
const [a,b, …arr] = [1,2];

// change code above this line
return arr;
}
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];


const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  "use strict";
  // change code below this line
  const [a,b, ...arr] = [1,2];
  
  // change code above this line
  return arr;
}
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements

So the line in your code:

…sets the value of a to 1, the value of b to 2, and the value of arr to… nothing at all. I honestly don’t see HOW this may have passed. running it in the console directly, or on jsfiddle, it fails. arr is returning as [] rather than a populated array.

The issue is, by hardcoding in [1,2], you have successfully disallowed any other values to be passed in. Your function is being given an array, passed as a param list – yet you never actually use that. You need to use the rest operator rather than using shift() in this challenge. Have you used it in the right place?

I am a newbie. I got the right answer from the Hint section, I’m just not sure why this line was accepted as correct.

You should be using the array which was passed into the removeFirstTwo function instead of hard-coding the [1, 2] array. Do you understand what variable you should be using as the array on the right side of assignment above?

At a guess, it was a bug. There is ABSOLUTELY NO reason why it should pass. If you echo a, b, arr within the function, you’ll see that arr is an empty array. So I lean towards you found a bug.

yes the answer is: const [,…arr] = list;

it is, yes. And when I ran your code it DOES pass. but oddly, if you watch teh console.log, you can see that arr is, in fact, [].

yes, I notice that when I copied and pasted the code in repl.it. []

Will they fix the bug?

One of the tests is checking the following assertion:

assert(arr.every((v, i) => v === i + 3));  // returns true because it is an empty array.

Well, THAT’S interesting. Would another likely assertion be to check that the returned length is two less than the original length? If that is the purpose of the function? In that way, even if the current assertion passes, the new one wouldn’t.

There are many other tests which would catch this problem. Maybe one of you would create an issue on Github for this? Just do a search first to make the same issue has not already been reported.

1 Like

Gentleman, I am a toddler trying to understand javascript, by doing two hours a day on your site. I hope to be half as good as you, in a couple of years.

lol @freedm55 – years and years at this, I’m still a noob. It’s all practice, and helping others. I’ve searched through the github, there is one other error reported for this particular task, but it’s a typo rather than an assertion issue. I’ll set up the issue, if you like. Unless you want to claim it, @freedm55, as you are the discoverer of this one.

Please set up the issue and claim it. I am a 52 year old beginner just hoping to someday be competent at this. Thank you for reaching out and helping.

Done and done: See your issue!

And I’ve been at this since 1986, in one form or another, and I’m still a work in progress. I’ll be 49 years young this year, and I’m excited about all the fun advances i see coming!! :wink:

Unless they can figure out how to extend human life for another 30 years, I am delusional to think I can become a web developer and change careers before I die. :exploding_head:

absolutely not. What you think may take thirty years can be done in three, if you have curiosity and a willingness to let go of ideas that may hold you back. The first of which is, “I can’t learn everything before I go!”

If you want to pursue the possibility of web development as a career change, freecodecamp is a great place to start. Also, get to some local meetups. Network, get to local events, see how others are thinking and what they’re doing. Challenge your thoughts every day. Tomorrow, you’re going to be a more mature coder than you are today.

Thanks, I appreciate the advice. On a good day, I can spend 3 hours of study before getting mental fatigue. I guess compounded 7 days a week for 3 years should yield some aha moments and eventually hire-able skills. :slight_smile:

Hell, a good WEEK can yield marketable skills. Have you SEEN the jobs being given to kids fresh outta high school? Bud, you and I got them beat with sheer life experience!

Yes I agree, but I’ll be honest, everything I ever set my mind to, I got good at, once I set my mind to it. However, this new challenge, of learning how to code (which is a ‘super power’ as far as I’m concerned) is very difficult to learn for me. I don’t know why but its freaking hard. I will not give up.