Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://www.jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Array.indexOf fix //JavaScript Function

Just an idiot fix to increase the number of my posts =b

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
[].indexOf || (Array.prototype.indexOf = function(v){
       for(var i = this.length; i-- && this[i] !== v;);
       return i;
});


example

var x = [0,1,2,3];

alert(x.indexOf(2));
alert(x.indexOf(4));

Array rotate //Javascript Function


Rotate the elements of an array with the minimum possible amount of movements.
It's thousands faster than using sequences of "array.unshift(array.pop())" or "array.push(array.shift())" due to memory moving on the "unshift" and "shift" methods. It also doesn't use a helper array, so it's fast and requires no aditional memory.

[UPDATED CODE CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/rotate [v1.0]

rotate = function(a /*array*/, p /* integer, positive integer rotate to the right, negative to the left... */){ //v1.0
    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;
};


Usage

document.write(
    "rotate([1,2,3], 2) = ", rotate([1,2,3], 2), "<br />",
    "rotate([1,2,3], -2) = ", rotate([1,2,3], -2), "<br />",
    "rotate([1,2,3], 1000) = ", rotate([1,2,3], 1000), "<br />"
)

getElementsAt //JavaScript Function

This function is able to get all the elements in a specific level of the array

example:

x = [-1, [[8, 22, -7], 32, [[4, 10, -2], 122]], 13];
alert(x.getElementsAt(3));



code:

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
Array.prototype.getElementsAt = function(level){
	var r = [], x = [], c = this, l = level, i;
	do
		if(i = c.length, !l)
			for(; i; r[r.length] = c[--i]);
		else
			for(; i; c[--i] instanceof Array && x.push(c[i], l - 1));
	while(l = x.pop(), (c = x.pop()) != undefined);
	return r;
}

Array Shuffle //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage

alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));


code based on Fisher-Yates (never heard about him hahaha) algorithm

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

Arrange //JavaScript Function


It arranges elements in an array, the "n" parameter determines the amount of elements in each combination and the "m" one is a boolean which says if the function should return an array with an "index map" or real values.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/arrange [v1.0]

arrange = function( v, n, m ){
 	for( var i, j, k, sep = sep || "", l = v.length, r = new Array( i = Math.pow( l, n ) ), c = ( new Array( n + 1 ) ).join( 0 ).split( "" ); i; )
		for( r[--i] = new Array( j = n ), k = 1; j--; r[i][j] = m ? c[j] : v[c[j]], k && ( ++c[j] != l && --k, c[j] %= l ) );
	return r;
};



Example

document.write( arrange( ["A", "B", "C" ], 3, 1 ).join( "<br />" ), "<hr />" );
document.write( arrange( ["A", "B", "C" ], 3, 0 ).join( "<br />" ) );

Permute //JavaScript Function


It permutes elements in an array, the "m" parameter is a boolean which determines if the function should return an array with an "index map" or the real value.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/permute [v1.0]

permute = function( v, m ){
	for( var j, l = v.length, i = ( 1 << l ) - 1, r = new Array( i ); i; )
		for( r[--i] = [], j = l; j; i + 1 & 1 << --j && ( r[i].push( m ? j : v[j] ) ) );
	return r;
};




Example

document.write( permute( ["A", "B", "C" ], 1 ).join( "<br />" ), "<hr />" );
document.write( permute( ["A", "B", "C" ], 0 ).join( "<br />" ) );
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS