javascript - I need to disorder an array -
hello need disorder array. tryed this:
var letras = ['a', 'e', 'i', 'o', 'u']; function disorder(p){ for(i=0;i<100;i++){ pos = math.random()*p.length; temp=p[pos]; p.splice(pos,1); p.push(temp); } }
but doesnt work, , not sure memory efficient because of deletes.
you need integer index.
pos = math.random() * p.length | 0;
the bitwise or cast value integer.
function disorder(p) { var i, pos, temp; (i = 0; < 100; i++) { pos = math.random() * p.length | 0; temp = p[pos]; p.splice(pos, 1); p.push(temp); } } var letras = ['a', 'e', 'i', 'o', 'u'], p = [] disorder(letras); document.write('<pre>' + json.stringify(letras, 0, 4) + '</pre>');
Comments
Post a Comment