www.webdeveloper.com
Page 10 of 15 FirstFirst ... 89101112 ... LastLast
Results 136 to 150 of 221

Thread: Here are 2 prototype-functions I created.

  1. #136
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    There are faster ways of shuffling array elements, but this is nice and short:
    Code:
    Array.prototype.shuffle=function(){
    return this.sort(function(){return [-1,1][Math.floor(Math.random()*2)]})
    }

  2. #137
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    I dont know why you would resort to such a laggy script, this is 40 more bytes but much more efficient, the one you provided gets exponentially worse with longer arrays

    Code:
    Array.prototype.shuffle=function(){var t=this,r=t.length,b=[],i=0,M=Math;while(i<r)b[i]=t[i++];while(i>0){r=M.floor(M.random()*i);t[--i]=b[r];b[r]=b[i]}return t}
    Last edited by BigMoosie; 08-26-2005 at 04:25 AM.

  3. #138
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    I was curious as to what the level of randomness these two functions were so I set up a test page:

    http://www.random.abrahamjoffe.com.a...huffletest.htm

    Standard deviation is the measurement we are after, close to zero is best, check the results yourself they are quite surprising.

  4. #139
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Geez, those results are shocking -- 1324 and 3124 got as high as x3.
    Last time I use the sort approach.

    I guess I will be forced to use your function or write a new one up.
    But I'm sure your method is both fast and accurate.

  5. #140
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    String/Number.changeBase(from, to);

    Changes a number from a base (first argument) to another base (second argument).

    Code:
    Number.prototype.changeBase=(
    	String.prototype.changeBase=function(){
    		return parseInt(this, arguments[0]).toString(arguments[1]);
    	}
    );

  6. #141
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    I have another revision of the rainbow script. It 's a String prototype function that creates DOM nodes (a containing span that contains one span per letter in the string). The reason for the containing span is so that another function can be called. This second function "moves" the rainbow by recoloring it with a different starting hue value.

    I'm sure it's less efficient than some of the other rainbow stuff we've made, but it moves! Ooh!
    Attached Files Attached Files
    Last edited by HaganeNoKokoro; 06-18-2005 at 02:45 AM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  7. #142
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Thats pretty awesome Hagane, though I dont see the purpose of making it a prototype function.

  8. #143
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    So I could post it in this thread
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  9. #144
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Quote Originally Posted by HaganeNoKokoro
    So I could post it in this thread
    LOL,
    I knew this was gonna happen sooner or later...
    That RainbowSpan function is a nice function and deserves a proper place. With all due respect, post it here: Function Library
    I'd add your RainbowSpan function to my index page, but it isn't a prototype function.
    You guys have been watching my first post of this thread, right?
    Last edited by Ultimater; 06-19-2005 at 12:55 AM.

  10. #145
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Yeah, I know it isn't really a prototype-function, I just figured I could get a rise out of you with that comment. I'll post the latest version in your other thread.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  11. #146
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Does anybody have a function that would turn:

    3.3333333333

    into

    [10,3]

    as in 10/3, and the same for any other rational?

  12. #147
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Thanks for the addition to my other thread, HaganeNoKokoro, that function really is an accomplishment -- I see many uses for it.

    BigMoosie, I'm sure I can come up with something -- my TI-86 calc has a ►Frac function.

  13. #148
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    3.3333333333 Can be written as: 33333333333 / 1000000000
    The only problem is that you need to make JavaScript understand that 3.3333333333 means 3.33... repeating.
    Thus 33333333333 / 1000000000 means 10/3
    Hmm....
    I'll have a clear head tomorrow...
    Last edited by Ultimater; 06-19-2005 at 04:13 AM.

  14. #149
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    It would be easier if you mark up the number to indicate repetition, something like: "3.[3]" where the part in brackets is the repeating part.

    In general:
    you have a string: a.b[c]
    where a, b, and c all consist only of digits 0-9

    This seems over-complicated, but:

    The fraction form would be equivalent to the sum of two fractions:

    The non-repeating part:

    (a*Math.pow(10, Math.ceil(Math.log(b)/Math.log(10)))+b)+" / "+(Math.pow(10, (Math.ceil(Math.log(a)/Math.log(10))+Math.ceil(Math.log(b)/Math.log(10)))))

    and the repeating part:

    c+" / "+((Math.pow(10, Math.ceil(Math.log(c)/Math.log(10)))-1)*(Math.pow(10, Math.ceil(Math.log(b)/Math.log(10)))));

    Example: given 3.4[123]
    a=3
    b=4
    c=123

    the result is: (34 / 100) + (123 / 9990)

    Of course, then you need something to add the fractions...
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  15. #150
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Dont follow what you mean but if you can get that to work, great!

    The method I always use is (mathematically):

    x = 5.142857 142857 142857 ... (given)

    1000000x = 5 142 857 . 142857 142857

    1000000x - x = 999999x = 5142852

    Therefore x = 5142852 / 999999

    which is symplifyable to:

    x = 36 / 7

    but that seems a of work to code... ill see what I can do.

    There is always the possibility of itteration, I think that would be the best way to go.

    Dont forget to cut off the last digit (if it is repeating) when taking the number as a string becasue that digit is sometimes rounded.

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles