Guide: Slasher Flick

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: Problem Explanation:

For example: slasher([1, 2, 3], 2); must return [3].

Relevant Links

:speech_balloon: Hint: 1

  • We need only the remaining part of an array, so we can just remove what we don’t.

try to solve the problem now

:speech_balloon: Hint: 2

  • splice() function can be used.

try to solve the problem now

:speech_balloon: Hint: 3

  • slice() function can be used.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

function slasher(arr, howMany) {
  // remove the head
  arr.splice(0, howMany);
  // return the remaining or the tail
  return arr;
}
slasher([1, 2, 3], 2);

:rocket: Run Code

Code Explanation:

  • This solution uses the splice() function.
  • First argument, arr is the array to be modified.
  • Second argument, howMany is the number of elements to be removed starting with arr[0].
  • Modify the array with splice() and return it.

:sunflower: Intermediate Code Solution:

function slasher(arr, howMany) {

  // Return string after the amount chopped off.
  return arr.slice(howMany);

}

:rocket: Run Code

Code Explanation:

  • This solution uses the slice() function.
  • The argument howMany is the number of elements to be removed starting with arr[0].
  • Modify the array with slice() and return it.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: 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. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.

7 Likes

Thanks Rafael, now if only someone can make a plug-in so I can filter all of the Algorithm Help posts when I am viewing all topics.

At this point it is border line spam just to copy/paste all of your solutions that are already in the wiki/on your github

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!

1 Like

We are moving all of our wiki pages to the forum.

@jaredabel thanks for your patience while this process continues.

@Rafase282 Thanks for taking the time to personally oversee this transition. Keep going on until the transition is complete.

Happy Coding

1 Like

@Rafase282

Apologies

No problem. I truly understand your position when you wrote that @jaredabel

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!

4 Likes

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.

Damn, something tells me javascript isn’t quite clicking with me yet. This is how I did it.

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  var slashed = arr.slice(0 + howMany -arr.length);

  if(howMany > arr.length){
    slashed = [];
    return slashed;
  }

  return slashed;
}
3 Likes

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]
1 Like

I am practicing with functional programming:

function slasher(arr, howMany) {
  return  arr.filter(function(item,index){
    return index > howMany-1; 
  });
}
3 Likes

My code:

function slasher(arr, howMany) {
  
  if(howMany > 0){
    
    for(var i=0; i<howMany; i++){
      arr.splice(0, 1);
    }
    return arr;
  }
  
  return arr;
  
}

//Test
slasher([1, 2, 3], 9);

Can some one tell me why this does not work?

function slasher(arr, howMany) {

return arr.splice(0,howMany)
}

slasher([1, 2, 3], 2);

4 Likes

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;

}
return i;
//return arr.splice(1, howMany);
}

slasher([1, 2, “chicken”, 3, “potatoes”, “cheese”, 4], 5);

function slasher(arr, howMany) {
// it doesn’t always pay to be first
for(i=0;i<howMany;i++)
{
arr.shift();
}
return arr;
}

2 Likes

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?

because by putting

return arr.splice(0, howMany);

you return the the portion of the array that we need to chopping off [1, 2].

but we need the remaining elements after chopping off that is [3].

so you must rewrite it as follow:

arr.splice(0, howMany);
return arr ;

As splice() modify the original array while slice() take a copy only without modifying the array.

Cheers!!

6 Likes