Basic algorithm scripting: Slasher Flick

Basic algorithm scripting: Slasher Flick
0.0 0

#1

Hello folk,
I don’t understand how .slice and .splice works…
Please share with me how you did yours…
Mine works;;;but i did it this ways:

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

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

#2

W3school.com is always a good place to get some more info. Here’s the link to the Javascript array reference: http://www.w3schools.com/jsref/jsref_obj_array.asp


#3

function slasher(arr, howMany) {

var output;

output = arr.slice(howMany, arr.length);
return output;
}

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

The way slice works is you give it a start point and an end point slice.(a, b) and it returns you that portion, and gets rid of the rest so to speak.

so for [1, 2, 3], to get just the 3, I know it starts 2 (because 0, 1, 2), that point is also going to be the same as the howMany input. So I used that. I also want to slice after that point, so it’d be 3, or the length of arr, arr.length which is 3. SO in short, you want the point the element starts and the point after.

Sorry, this is probably poorly explained.

Another eg. if you had an array, array = [1, 2, 3, 4, 5] and you wanted to slice out 3 and 4, you’d go array.slice(2, 4)
because [1, 2, (point 2) 3, 4,(point 4) 5]

if I just did arr.slice(2, 3), I’d only get 3 because[1, 2, (point 2) 3, (point 3) 4, 5]


#4

You could just write “output = arr.slice(howMany);” since slice will continue till the end :slight_smile:


#5

Sometimes even the simplest things tend to get very difficult if we don’t know the basics. I suggest that you go through Basic Javascript in FCC module completely before you attempt Algorithms questions. All these topics are well covered in this module.

Please don’t misinterpret as me being rude to you. I have faced these issues many times (I still do). It helps to go over the basics especially if you have time.

Good luck :thumbsup:

Here is my solution.


function slasher(arr, howMany) {
  // Slice the array from the given position till the end of an array
  return arr.slice(howMany, arr.length);
}

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



#6
function slasher(arr, howMany){
  return arr.slice(howMany,arr.length);}