Magic rotate Function

Source: Internet
Author: User
I want to introduce you to a magic rotate or array flip function. Let's look at an example: I want to introduce you to a magic RotateOr the array flip function, let's look at an example:


var data = [1, 2, 3, 4, 5];rotate(data, 1) // => [5, 1, 2, 3, 4]rotate(data, 2) // => [4, 5, 1, 2, 3]rotate(data, 3) // => [3, 4, 5, 1, 2]rotate(data, 4) // => [2, 3, 4, 5, 1]rotate(data, 5) // => [1, 2, 3, 4, 5]

Did you see the portal? I didn't see any relationship. Let me explain it.

Take the first oneRotate (data, 1)For example, it is equivalent that all the elements from the first to the last to the second move one to the right, and the last element draws a half-month arc and runs to the first position.

WhileRotate (data, 2)InRotate (data, 1).

Rotate (data, 3) based on rotate (data, 2 .....

This is not complete yet,RotateThe function has more powerful functions. What if I try to input a negative number?


rotate(data, -1) // => [2, 3, 4, 5, 1]rotate(data, -2) // => [3, 4, 5, 1, 2]rotate(data, -3) // => [4, 5, 1, 2, 3]rotate(data, -4) // => [5, 1, 2, 3, 4]rotate(data, -5) // => [1, 2, 3, 4, 5]


You may soon find out that this isRotateThe opposite process.

If0What will happen?


rotate(data, 0) // => [1, 2, 3, 4, 5]

Flip0Time, it's not that you do nothing, Khan ^_^

This function is almost omnipotent:

The number array can be used, and other object arrays can also be flipped.


rotate(['a', 'b', 'c'], 1)     // => ['c', 'a', 'b']rotate([1.0, 2.0, 3.0], 1)     // => [3.0, 1.0, 2.0]rotate([true, true, false], 1) // => [false, true, true]

No problem how many times I want to flip! Even tens of thousands of times!


var data = [1, 2, 3, 4, 5]rotate(data, 7)     // => [4, 5, 1, 2, 3]rotate(data, 11)    // => [5, 1, 2, 3, 4]rotate(data, 12478) // => [3, 4, 5, 1, 2]

Well, after reading so many amazing features, let's think about how to implement it.
Since there are three basic conditions: positive number, negative number, and zero, let's break them apart!

For positive numbers, write this function specifically:


if(typeof Array.prototype.shiftRight !== "function"){    Array.prototype.shiftRight = function(n){      while(n--){        this.unshift(this.pop());      }    };}

Then, the negative number depends on the function opposite to this function:


if(typeof Array.prototype.shiftLeft !== "function"){    Array.prototype.shiftLeft = function(n){      while(n--){        this.push(this.shift());      }    };}


Finally, we only need to integrate it,RotateThe function comes out:

function rotate(array,n){    //copy array    array = array.slice(0);    if(n > 0){        array.shiftRight(n);    }    else if(n < 0){        array.shiftLeft(-n);    }    return array;}


The above is the magic rotate function content. For more information, please follow the PHP Chinese Network (www.php1.cn )!

Related Article

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.