JavaScript algorithm exercises: Slasher Flick

Source: Internet
Author: User

The simple point is that the given array is intercepted. It looks like the array element is deleted, and you might think of the method in the array shift() , but this method can only delete the first element of the array. The function we are going to implement is to intercept the given array according to the second parameter, and if the second argument is greater than the length of the array, an empty array is returned, otherwise the second parameter is returned as an array of all the elements after the index value.

This is done in JavaScript. Create a slasher() function and pass two arguments to the function arr and howMany . Then return the truncated array:

function slasher(arr, howMany) { // 这里添加处理方法 return arr;}
Implementation ideas

According to the above, the realization slasher() of the idea:

    • If howMany equal 0 , returns the original arrayarr
    • If it is howMany greater than arr.length or equal, the method using the array will arr delete all items inside, and an empty array will be returned[]
    • In other cases, arr remove the element from the first element to howMany the reference, and return a new array

To implement these functions, you can use the shift() , slice() and methods in the array splice() . Simply recall:

    • Array.prototype.shift(): Deletes the first element of the array and returns the element
    • Array.prototype.slice(): Extracts a subset of the elements from the array into a new array object and returns the new array
    • Array.prototype.splice(): Modifies the contents of an array by searching for the old element with the new element

Like what:

var arr = [1,2,3,4,5,6,7,8,9];console.log(arr.shift()); // 1console.log(arr.slice(0,3)); // [1, 2, 3]console.log(arr.splice(0,3)); // [1, 2, 3]
Implementation scenarios

Also mentioned at the beginning of the article, the realization slasher() function function, through the array shift() , slice() and splice() methods can be achieved. Next, look at how these methods are implemented.

Method 1:shift ()

The array shift() method deletes the first element of the array, which is far apart from the elements we need to delete the array howMany . Does not seem to achieve the same, but in fact, with the for loop can achieve the function we need:

function Slasher(arr, Howmany) {for (var i=0; i < Howmany; i++) {arr.shift ();arr = [1,2,3,4,5,6,7,8,9] howmany=3Number of traversal i=? I1st 0 Yes 1 1 [2,3,4,5,6,7,8,9]2nd 1 Yes 2 2 [3,4,5,6,7,8,9]3rd 2 Yes 3 3 [4,5,6,7,8,9]4th 3 No}return arr;} Slasher ([1,2,3,4,5,6,7, 8,9],3); //[4, 5, 6, 7, 8, 9]slasher ([1,2, 3,4,5,6, 7,8,9],0); //[1, 2, 3, 4, 5, 6, 7, 8, 9]slasher ([1,2,3,4,5,6,7,8,9],10); //[]               
Method 2:slice ()

slice()Method can intercept a subset of the elements in an array and deposit them into a new array object. and returns the new array. In this way, in a slasher() function, you can do this:

    • If howMany equal 0 , returns the original arrayarr
    • If howMany greater than or equal arr.length , the arr.slice(arr.length) element is arr not taken from, an empty array is returned[]
    • In other cases, the arr.slice() arr inner element is -arr.length + howMany counted to intercept the last element of the array and return

Then the slasher() function can be written like this:

function Slasher(arr, Howmany) {var len = arr.length;if (Howmany = = =0) {return arr; }Elseif (Howmany >= len) {Return Arr.slice (len); }else {Return Arr.slice (-len + howmany); }}slasher ([1,2,3,4,5,6,7, 8,9],0); //[1, 2, 3, 4, 5, 6, 7, 8, 9]slasher ([1,2,3,4,5,6,7,8,9],10); //[]slasher ([1,2,3,4,5,6,7,8,< Span class= "number" >9],4); //[5, 6, 7, 8, 9]             
Method 3:splice ()

splice()The method can specify how many elements are removed from the first element of the array, and then return the modified array. That

    • If howMany equal 0 , returns the original arrayarr
    • If it is howMany greater than arr.length or equal, Array.splice() arr all elements inside will be deleted, and an empty array will be returned[]
    • In other cases, use Array.splice() the arr elements from the first element to the howMany specified element, and return the modified array

The code is as follows:

function Slasher(arr, Howmany) {var len = arr.length;if (Howmany = = =0) {return arr; }Elseif (Howmany >= len) {Arr.splice (0, Len);return arr; }else {Arr.splice (0, Howmany);return arr; }}slasher ([1,2,3,4,5,6,7,8,9],0); //[1, 2, 3, 4, 5, 6, 7, 8, 9]slasher ([1,2, 3, 4, 5,6,7,8,9],10); //[]slasher ([1,2,3,4,5,6,7,8,9],4); //[5, 6, 7, 8, 9]                
Summarize

The article uses arrays, Array.shift() Array.slice() and Array.splice() methods to implement the given array, according to the second parameter interception operation, if the second argument is greater than the length of the array, then returns an empty array, otherwise the second parameter is returned as an array of all elements after the index value.

JavaScript algorithm exercises: Slasher Flick

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.