UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

111 lines (84 loc) 2.79 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">????? ???????</a> </strong> on 2012-04-10 09:55:00 <br /> If I might —perhaps you should consider adding a few images. I don’t mean to disrespect what you’ve said ; its very enlightening, indeed. However, I think would respond to it more positively if they could be something tangible to your ideas <hr /> <strong> <a href="http://bi3.biz" rel="nofollow">Eugen</a> </strong> on 2011-04-21 23:23:19 <br /> one example here http://javascript.about.com/library/blsort2.htm <hr /> <strong> Taai </strong> on 2011-02-08 10:58:52 <br /> I improved this code. <pre><code> function array_rand(input, num_req) { var input_len, rand, indexes = [], ticks = num_req || 1; if (input instanceof Array === false || ticks &gt; (input_len = input.length)) { return null; } while (indexes.length != ticks) { rand = Math.floor(Math.random() * input_len); if (indexes.indexOf(rand) == -1) { indexes.push(rand); } } return ticks == 1 ? indexes[0] : indexes; } </code></pre> Some (older) browsers doesn't support Array.indexOf function, so, here is a code (wich I also improved) for those older browsers (add this code at least before array_rand): <pre><code> if (!Array.indexOf) { Array.prototype.indexOf = function(obj) { var i = 0, l = this.length; for (; l--; i++) if (this[i] == obj) return i; return -1; } } </code></pre> Have fun! <hr /> <strong> pwoul </strong> on 2009-05-10 03:30:30 <br /> thanks a lot, that's working now :) (here's the code if someone needs it) <pre><code> var result = &quot;&quot;; alea = array_rand([&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;], 8); for(var i in alea) { var kikoo = alea[i]; result = result + tableau[kikoo]; } </code></pre> <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-05-09 23:18:59 <br /> @pwoul, You are referencing an array/object called &quot;tableau&quot; instead of the one you randomized, &quot;alea&quot;. Change it to &quot;alea&quot; (if that's what you want), and the number will vary... best wishes, Brett <hr /> <strong> pwoul </strong> on 2009-05-09 13:25:38 <br /> Hi, first i'd like to thank you for this very nice function. But (there's always one :D) i have a problem using it : <pre><code> var result = &quot;&quot;; var alea = array_rand([&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;], 8); for(var i in alea) { result = result + tableau[i]; } </code></pre> This code always returns the string &quot;abcdefgh&quot;, the order of the characters isn't random :/ Thanks for your help :) <hr />