View Full Version : Pointerless Array Rotation without use of Indexes

01-29-2012, 09:57 AM
In many cases the use of an 'array pointer' is literally 'pointless' ... :)

A good example would be an array of elements that is nothing more than an array for banner image url's. Using a "rotate & Return" method of working would mean that you can reduce the need to "Track" the array pointer, the most current element is returned from the array whilst being preserved in the array.

This does not stop you adding elements to the array, should you wish a new element to be the very next used, you would unshift() the element otherwise you would just push() the element before it is called.

// Get the first element in the array, removes it and appends it to the array and then returns the rotated element.
Array.prototype.returnFirstAndRotate = function(){
var tmp = this.shift();
this.push( tmp );
return tmp;
array = [3,1,2];
x = array.returnFirstAndRotate();

Will return the number 3 which has now been pushed on to the end of the array, the array is still 3 elements long but now is [1,2,3] until the next call of x = array.returnFirstAndRotate();

I do have the alternative of rolling back the array, seen here...

// Array roll back
Array.prototype.rollback = function(){
var tmp = this.pop();
this.unshift( tmp );
return tmp;
// eg.
array = [3,1,2];
x = array.returnFirstAndRotate();
alert( array.join(",") );
x = array.rollback();
alert( array.join(",") );

Will return the last array element value and push it on to the front of the array, x will equal 3 in this example. and we end up with an array of [3,1,2]

Philip M
01-29-2012, 11:50 AM
Here is another way:-

<script type = "text/javascript">

rotate = function(a, p){
for(var l = a.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
for(i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x);
return a;

x = rotate([1,2,3,4,5,6,7,8,9,10], 2);
alert (x);
x = rotate(x,2);
alert (x);
x= rotate (x,7);
alert (x);


01-29-2012, 12:18 PM

I have added some more prototypes that may prove useful to someone looking to manipulate arrays and obtain information or data.

// remove first element and return it or return false
Array.prototype.removeAndReturnFirst = function(){
return this.length ? this.shift() : false;
// remove last element and return it or return false
Array.prototype.removeAndReturnLast = function(){
return this.length ? this.pop() : false;
// remove an element by ref
Array.prototype.removeAndReturn = function(ref){
return this.length && this[ref] ? this.splice(ref,1) : false;
// remove and append an element by ref - you might want to move element[x] to the bottom of the pile! but still return its data.
Array.prototype.removeAndAppend = function(ref){
var tmp = this.length && this[ref] ? this.splice(ref,1) : false;
if( tmp ) this.push( tmp );
return tmp;
// Rotate an array of elements by the number passed and return that value.
Array.prototype.rotateArrayBy = function(n){
var tmp=false, n = n || false;
if( n && this.length )
while( --n >= 0){
tmp = this.shift();
this.push( tmp );
return tmp;

false is returned if the array is empty, allows for testing the return value otherwise the array element / data is returned.

The rotateArrayBy() method will remove n elements and then return the value of the n'th element not the first element in the array, eg...

array = [3,4,5,6,7,8,9,10,1,2];
x = array.rotateArrayBy(6);
alert( x );
alert( array.join(",") ); will result in [9,10,1,2,3,4,5,6,7,8] and the value of the n'th element was 8.

01-29-2012, 12:31 PM
Also here is an interesting method of either or gathering of an element.

// remove an element by ref or rotate array ref elements and return n'th element
Array.prototype.removeAndReturnByOrRotateToNth = function(ref){
var tmp=false;
if( this.length && this[ref] ){
tmp = this.splice(ref,1);
this.rotateArrayBy(ref); // the n'th will be the last element!
tmp = this.pop();
return tmp;

array = [1,2,3,4,5,6,7,8,9];
x = array.removeAndReturnByOrRotateToNth(33);
alert( x );
alert( array.join(",") ); Returns [7,8,9,1,2,3,4,5] and the n'th = 6.

The function will remove the n'th element from an array regardless of n'th existing or not. Will rotate the array by n'th elements instead and remove the last element which will be the target we want as it will be the n'th element in that array.

01-29-2012, 08:15 PM
Interesting idea. I think that the better is to shorten names of methods in your first post like these:


Look here: the returnFirstAndRotate method has well-defined name but too long and wordy. The rollback method is the inverse of the previous one. Ok -- it is shorter but we don't know what we rollback and what is the direction of rolling back.

Anyway, starting to use one or both methods we have to read documentation of both methods so the better way is to give them names looking alike and describe what do they do and what do they return.


Talking about removeAndDoSomething methods' family

false is returned if the array is empty
Use the 'undefined' value instead. Check the result of [].pop() or [].shift() to find out the 'undefined' value returned by these methods. So removeAndReturnFirst/removeAndReturnLast can be reduced until the native methods shift/pop.

Consider this -- this.length && this[ref]. What about this[ref] == 0, false, '', NaN, null, undefined. What means that item is 'out of bound'?

The last thing in my critics. Maybe this code below can be useful in the future :-) But it requires to be improved...

Array.prototype.rotate = function(n)
return this.push.call(this, this.splice(0, n));

Sorry for this my wordy post. I'd like to be make your ideas slightly better if it is possible anyway.

01-29-2012, 09:06 PM
Like it!

The more the merrier I say.

I do try to over simplify my examples without being too dumb if you follow. The scripts may appear rough and lacking but thats the point of a base to start from. Nothing in my coding is "Fixed" if I can get away with it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum