The argument howMany is the number of elements to be removed starting with arr[0].
Modify the array with slice() and return it.
NOTES FOR CONTRIBUTIONS:
DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
Add an explanation of your solution.
Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.
Please add your username only if you have added any relevant main contents. ( DO NOTremove any existing usernames)
They will go away eventually as more stuff is created. We are moving the wiki over here and removing it from the other places, FCC wiki is not the same as my personal one. I’m in charge of both yes, and originally they came from mine but I made sure the one for FCC was not exactly the same.
With that said, there will be more wiki articles coming up, so brace yourself, Summer is here!
I don’t want to sound like nitpicking but I would like to note that the challenge states:
“Return the remaining elements of an array after chopping off n elements from the head.”
So this suggests you modify the original array and return it. slice() does not modify the original array. To me, splice() is the more accurate solution to this challenge.
Cheers and thanks for posting all these solutions – it’s great contribution!
Are you saying that splice should be the intermediate solution, and slice be the basic? I would disagree. The challenge says “Return the remaining elements of an array after chopping off n elements from the head.” What makes you think that the challenge says to modify the original array? The challenge does not state “Take n elements off an array, and return the array”: it just says to return the elements off of the array… this sounds like slice to me. Also, the slice solution is a easier solution than splice.
Bottom line, they are both so similar, does it really matter either way? I think the wiki is fine as it is.
You chop the head and tail of the fish and you give the remaining of the fish to the customer. That’s how I interpret the statement: “Return the remaining elements of an array after chopping off n elements from the head.”
“it just says to return the elements off of the array”. No, it says returning the remaining elements of the array, after you chopped the head; not “return the chopped elements of the array”, which is what the solution with slice() does.
Why are these 2 different?
EDIT: I’ve long since found the answer - amazing how much I’ve learned since then. However, thank you for everyone who has responded. <3 this community!
function slasher(arr, howMany) {
arr.splice(0, howMany);
return arr;
}
slasher([1, 2, 3], 2);
// Returns [3]
function slasher(arr, howMany) {
var body = arr.splice(0, howMany);
return body;
}
slasher([1, 2, 3], 2);
// Returns [1,2]
how about this ( it worked but i feel it a bit stupid )
function slasher(arr, howMany) {
// it doesn’t always pay to be first
var i=0;
var lis=[];
if(howMany===arr.length-1){
arr.splice(0, howMany);
return arr;
}
else if (howMany===0){
return arr;
}
else if (howMany>arr.length){
return lis;
}
else{
while(i<arr.length){
while(i<howMany){
i++;
}
lis.push(arr[i]);
i++;
}
return lis;
I use this solution too at first, but it doesn’t return the right answer. It returns the removed items instead the remainder. But after I put the return in the end, it returns the right answer. Why?