# Slasher Flick - is it okay? (SPOILERS ALERT)

Firstly I did this challenge without .splice(), but then I changed it so that I do.
Do you think any of these solutions is better than the other or perhaps you have a third solution which would be best fit? Thanks forward for your answers!

My solution 1:

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

My solution 2:

``````
function slasher(arr, howMany) {
// it doesn't always pay to be first
for (var i = 0; i < howMany; i++) {
arr.splice(0, 1);
}
return arr;
}
``````

Hey, @Milos2709. Not bad! If I didnâ€™t know about `splice` Iâ€™d probably do it like your first solution. The second one seems like a waste of `splice`, since you can specify in it how many items you want to remove.

[details=This is mine]

``````function slasher(arr, howMany) {
// it doesn't always pay to be first
arr.splice(0, howMany);
return arr;
}
```[/details]``````

Really makes sense, @kevcomedia ! Thanks for the tip. I am still completely new to the .splice(), it actually took me a few minutes of investigation to understand what all the arguments mean.

this was my answer and you can see it also

Hi there. So I have solved this but I am looking for an explanation as to why a couple thing do not work.

First.
function slasher(arr, howMany) {

`````` var newArray = arr.splice(0,howMany);
return newArray;
}
slasher([1, 2, 3], 0);
``````

The above does not work but yet arr.splice(0,howMany); does. I dont understand how one works but one doesnâ€™t. I thought I was just assigning the splice to newArray.

If I even then follow it up with newArray = arr; it suddenly works? That does not make any sense to me.

Second why does this not work?

``````function slasher(arr, howMany) {
// it doesn't always pay to be first

var DelCount = howMany + 1;
var newArray = arr.splice(howMany,DelCount);

if (howMany === 0){

return arr;
} else {

return newArray;
}

}

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

So this one refuses to return arr. When I run this it returns [2,3] which should not happen at all.
A. Arr should be equal to [1,2,3] HOWEVER if I throw a return = arr; at the top to check, it says arr = [2,3] which it shouldnâ€™t. I dont know if this is a bug or what is going on. It refuses to actually read arr correctly.

Anyway any help as to why these might not work would be great.

the explanation is that splice modify the array by deleting a number of element specified by the 2nd argument and return " the elements deleted". so the content of the array after modification and the returnd array from the splice method will be diffent.

for example

var arr = [2, 5, 4, 7];
arr.splice(0,2);
console.log(arr)// will output [4, 7]

var arr = [2, 5, 4, 7];
var deletedValues = arr.splice(arr);
console.log(deletedValues) // will output [2,5]

so splice modify the array by deltening a number o elements and return the deleted elemnts

refer to the documentation for more detailed. and sorry for my english

2 Likes

`splice` modifies the array in-place, and it returns an array of removed elements. For example,

``````var arr = [2, 3, 4, 5];
var newArr = arr.splice(0, 2); // the first two elements will be removed, and assigned to `newArr`
// `arr` now is [4, 5].
// `newArr` now is [2, 3].  This is what you were returning.
``````

This works because `newArr` now refers to the same array that you `splice`d. But thatâ€™s redundant.
Just return the original array.

The problem is to remove the first `n` elements from the array. `splice`'s first argument is the index from which weâ€™ll start removing elements (in this case, `0`, since itâ€™s where the start is). The second argument is the number of elements to be removed (in this case, the value of `howMany`, which is quite obvious). `arr.splice(howMany,DelCount)` is simply wrong for this problem.

`Array.prototype.splice()`

2 Likes

Thanks for all the feedback. I really appreciate it!

Iâ€™m quite proud of this one. Very simple to understand. only one line of added code + a new return value.

//Slasher Flick
function slasher(arr, howMany) {
// Since the howMany is the beginning number. arr.length should be the end.
newArr = arr.slice(howMany, arr.length);

return newArr;
}

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

1 Like

Here is mine:

``````return arr.splice(howMany);

``````

Really struck by how simple and beautiful this stuff can be.

3 Likes

i did the came thingâ€¦ lol

My work in progressâ€¦

function slasher(arr, howMany) {
// it doesnâ€™t always pay to be first
var n = arr[arr.length - 1];
for(var i = 0; i < howMany; i++) {
return arr.slice(n + 1);
}
return arr;

}

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

I wonder if I have missed something as the solution is really simple:

function slasher(arr, howMany) {
// it doesnâ€™t always pay to be first
return arr.splice(howMany);
}

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

In fact, it still works/passes the tests if I use â€śsliceâ€ť in the same way.

Just one line syntax using slice-

``````
function slasher(arr, howMany) {
// it doesn't always pay to be first
return arr.slice(howMany);
}

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

``````

I followed another way of thinking, pushing into a new array the elements needed for the solution.

``````function slasher(arr, howMany) {
var array = [];
for (i = howMany; i < arr.length; i++) {
array.push(arr[i]);
}
return array;
}

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

Hope these helped.
Or maybe it is too nooby!

hi ,
this is my code ( a simple one)
function slasher(arr, howMany) {
// it doesnâ€™t always pay to be first
for (i = 0; i<arr.length;i++){
if (howMany==arr[i]){
return [];
}else{
return arr.slice(howMany, arr.length);
}
}
return arr;
}

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