# freeCodeCamp Algorithm Challenge Guide: Slasher Flick

freeCodeCamp Algorithm Challenge Guide: Slasher Flick
0.0 0

#1

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

### Problem Explanation:

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

## 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

## Hint: 2

• `splice()` function can be used.

try to solve the problem now

## Hint: 3

• `slice()` function can be used.

try to solve the problem now

## Basic Code Solution:

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

### 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.

## Intermediate Code Solution:

``````function slasher(arr, howMany) {

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

}
``````

### 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.

## 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.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

#2

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

#3

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!

#4

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

#5

@Rafase282

Apologies

#6

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

#7

#8

#9

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!

#10

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.

#11

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.

#12

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;
}``````

#13

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]``````

#14

I am practicing with functional programming:

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

#15

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);``````

#16

Can some one tell me why this does not work?

function slasher(arr, howMany) {

return arr.splice(0,howMany)
}

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

#17

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);

#18

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

#19

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?

#20

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!!