www.webdeveloper.com
Page 8 of 15 FirstFirst ... 678910 ... LastLast
Results 106 to 120 of 221

Thread: Here are 2 prototype-functions I created.

  1. #106
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Here's a couple:

    oldie but a goodie
    String.prototype.pad
    - allows you to specify a character (c) and pad a string to a certain length (n) with that character
    Code:
    String.prototype.pad = function(c, n) {
      var result=this;
      while(result.length<n) {
        result=c+result;
      }
      return result;
    }
    useless but fun
    String.prototype.toRainbow
    - returns HTML code for a rainbow version of the string by placing each letter in a differently colored span. Note: it would be pretty easy to modify this to create DOM nodes to be dynamically added into a document. The way it is now, you'd have to use either document.write or the hated innerHTML.
    Code:
    String.prototype.toRainbow = function() {
      var result="";
      var mode=0;
      var msg=this;
      var rr=0;
      var gg=0;
      var bb=255;
      ink=765/msg.length; //this ensures the whole string will go from blue to red to green back to blue
      for(var i = 0; i < msg.length; i++) {
        var theColor=Math.round(rr).toString(16).pad("0", 2)+""
                    +Math.round(gg).toString(16).pad("0", 2)+""
                    +Math.round(bb).toString(16).pad("0", 2);
        result+="<span style=\"color:#"+theColor+"\">"+msg.charAt(i)+"<\/span>";
        switch(mode) {
          case 0: //blue to red
            bb-=ink;
            rr+=ink;
            if(rr>=255) {
    	  rr=255; 
              bb=0;
              mode=(mode+1)%3;
            }
          break;
          case 1: //red to green
            rr-=ink;
            gg+=ink;
            if(gg>=255) {
    	  gg=255; 
              rr=0
              mode=(mode+1)%3;
            }
          break; //green to blue
          case 2:
            gg-=ink;
            bb+=ink;
            if(bb>=255) {
    	  bb=255; 
              gg=0;
              mode=(mode+1)%3;
            }
          break;
        }
      }
      return result;
    }
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  2. #107
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    String.prototype.toRainbow(); ...revised

    This is faster

    Code:
    String.prototype.toRainbow = function() {
    	var a=this.length,c,s="",x;
    	for (var i=0; i<a; i++) {
    		x=3*i/a;
    		c=(((x>2)?0:Math.round(255*(1-Math.abs(x-1)))*65536)+((x<1)?0:Math.round(255*(1-Math.abs(x-2)))*256)+((x>1&&x<2)?0:Math.round(255*(Math.abs(x-1.5)-0.5)))).toString(16);
    		while(c.length<6)c="0"+c;
    		s+='<SPAN style="color:#'+c+';">'+this.charAt(i)+'</SPAN>';
    	}
    	return s;
    }
    Last edited by BigMoosie; 06-01-2005 at 01:42 AM.

  3. #108
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Interesting, and pretty slick. Also interesting, I get slightly better results yet with
    Code:
    String.prototype.toRainbow = function() {
      var result="";
    
      var color;
      var mark1=Math.floor(this.length/3);
      var mark2=2*mark1;
      var a=this.length;
      for(var i=0; i<mark1; i++) {
        color=Math.floor((3*i/a)*255);
        red=color;grn=255-red;blu=0; //green to red
        result+="<span style=\"color:rgb("+red+", "+grn+", "+blu+")\">"+this.charAt(i)+"<\/span>";
      }
    
      for(var i=mark1; i<mark2; i++) {
        color=Math.floor((3*i/a)*255);
        blu=color-255;red=255-blu;grn=0; //red to blue
        result+="<span style=\"color:rgb("+red+", "+grn+", "+blu+")\">"+this.charAt(i)+"<\/span>";
      }
    
      for(var i=mark2; i<a; i++) {
        color=Math.floor((3*i/a)*255);
        grn=color-511;blu=255-grn;red=0; //blue to green
        result+="<span style=\"color:rgb("+red+", "+grn+", "+blu+")\">"+this.charAt(i)+"<\/span>";
      }
    
      return result;
    }
    On my computer, testing on a really long string, mine runs in 156-172 ms, and yours runs in 172-187 ms.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  4. #109
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    You are partially correct, I am guessing you tested it in a Gecko browser? In internet explorer the results are the other way around.

  5. #110
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    And sorry, but I just had to improve your pad function, the two problems you had that make mine up to four times faster were on each iteration of the loop you were checking result.length, that drains more resources than just checking a variable. Also, you added to a (possibly) already long string each time, this one will concatenate the pad and "this" only at the end rather than on each iteration. I doubt anybody will use this function on a such a long string anyway.

    Code:
    String.prototype.pad=function(c,n){
    	var t="";
    	for (var i=this.length; i<n; i++) t+=c;
    	return t+this;
    }

  6. #111
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Hey guys,

    I wanted to jump in here regarding the discussion about the JavaScript Source site (I didn't catch that part when it was current).

    As the Manging Editor, I am always open for new submissions. Ultimater, for some reason I did not receive your script. I would encourage you to re-submit it.

    The scripts are used by those who have no understanding and others who do. Many times, what you think are useless scripts are posted more for learning than actual use or for adapting into other scripts.

    As to the quality of scripts, I work with what is submitted. There are many excellent scripts on the site. I would challenge those of you here to submit your work to JSS. I have seen what you guys can do. Show the rest of the world! The scripts are also featured in the JavaScript.com newsletter (as well as on that site also), not to mention they are sometimes also featured in the WebReference.com newsletter. (In addition, the JSS and JS sites have been mentioned in several recently published books on JavaScript and HTML.) That's quite a bit of coverage on the Web! (Even this library of functions is useful to others.)

    (BTW, it only takes a minute or two to submit the scripts. As much time as it takes to submit something to the Coffee Lounge, ;-> )
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  7. #112
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Well LeeU, now that we have your attention, I really suggest you remove this script:

    http://javascript.internet.com/calcu...of-primes.html

    I do not know who approved it but it really is pathetic, it does not do what it says it does, it just returns all the odd numbers up to it. It is an embarassment to your website.

    I suggest you replace it with this lovely script of mine :

    http://www.random.abrahamjoffe.com.a...pts/primes.htm

    It tells you whether or not a number is prime and also breaks it into its prime factors.
    Best of all it has a loading bar for big numbers like: 100546345345344

    Ill go submit it now...

    EDIT: this doesnt use my more powerful engine that I submitted on this thread, but in the meantime it does what it should, I may integrate the faster code at a later date. It takes a bit of effort as you have to break it into an asynchronous code which can kind of ruin the structure.
    Last edited by BigMoosie; 06-01-2005 at 09:46 AM.

  8. #113
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Ah, now we're talkin'!

    I will be going thru the site in a few months (once I get a better handle on JS) and changing/updating scripts. If you guys have any suggestions for updating (and don't forget the new ones), let me know! You can e-mail me directly at: lunderwood@jupitermedia.com (with your comments, not your scripts - submit them online).

    BTW, I'm going to be setting up a function/snippet library section on the site. These look like some good candidates here... (I'll give proper credit, of course ;->).
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  9. #114
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Moosie, that's interesting about the pad function. I can certainly understand the problems with concatenating over and over again to a long string, but I didn't realize String.length was so slow. Does it walk through the string counting up (yuck), or does it just plain take longer to access object properties than standalone variables?

    As for the rainbow, the results I posted were from a Gecko browser. In IE, there seemed to be almost no difference between the two. Strangely enough, when I made the string ridiculously huge (length=11329), Gecko performance was still pretty good (almost invariably 1250ms for mine, usually 1350-1450ms for yours) but IE performance suffered horribly (9950ms for mine, 7500ms for yours).

    Also, yours seemed to encounter a bit of rounding error (or something) around 2/3 through the string (a couple of letters turned black, for some reason)
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  10. #115
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Quote Originally Posted by BigMoosie
    I suggest you replace it with this lovely script of mine :

    http://www.random.abrahamjoffe.com.a...pts/primes.htm

    It tells you whether or not a number is prime and also breaks it into its prime factors.
    Best of all it has a loading bar for big numbers like: 100546345345344

    Ill go submit it now...

    EDIT: this doesnt use my more powerful engine that I submitted on this thread, but in the meantime it does what it should, I may integrate the faster code at a later date. It takes a bit of effort as you have to break it into an asynchronous code which can kind of ruin the structure.
    That is really an impressive script moosie, It even has an accurate "thinking" bar.

    Try typing-in "68688969678967".

  11. #116
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Mossie,

    Have you submited it yet ... if so, I haven't received it. I want to make sure I receive it.
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  12. #117
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by HaganeNoKokoro
    Moosie, that's interesting about the pad function. I can certainly understand the problems with concatenating over and over again to a long string, but I didn't realize String.length was so slow. Does it walk through the string counting up (yuck), or does it just plain take longer to access object properties than standalone variables?

    Also, yours seemed to encounter a bit of rounding error (or something) around 2/3 through the string (a couple of letters turned black, for some reason)
    I'm not entirely sure why it is faster to look up variables than properties, probably for the reason you suggested, that it has to solve the length each time. And as for that error, I cannot see why that would occur, I am at school (software class and no teacher) so I cannot test it but if you find an error you should post what you tested and what result you got.

    Quote Originally Posted by Ult'
    That is really an impressive script moosie, It even has an accurate "thinking" bar.
    Thankyou it "jumps" every time it finds a new factor but otherwise it is accurate.

    Quote Originally Posted by LeeU
    ...haven't received it...
    I have sent it.

  13. #118
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Here's the file I'm testing in, Moosie. As said, it's using an absurdly long string. The problem occurs in both IE and Netscape. The file contains both your function (called "toRainbow2") and my function (called "toRainbow3"). It is currently set to call your function.
    Attached Files Attached Files
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  14. #119
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Hey, I made two adjustments to the rainbow code, firstly it now scrolls through the entire hue spectrum and secondly it outputs BBcode!


  15. #120
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    HaganeNoKokoro, that prototype function is very cool.
    BigMoosie, can you share the changes you made to it?
    Last edited by Ultimater; 06-05-2005 at 02:41 PM.

Thread Information

Users Browsing this Thread

There are currently 10 users browsing this thread. (0 members and 10 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