www.webdeveloper.com
Page 7 of 15 FirstFirst ... 56789 ... LastLast
Results 91 to 105 of 221

Thread: Here are 2 prototype-functions I created.

  1. #91
    Join Date
    Jul 2004
    Location
    web develepment land
    Posts
    164
    ok and also sorry about that

  2. #92
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    I blogged about a prototype for comparing colors and changing hex to rgb here.

    http://radio.javaranch.com/pascarell...513366466.html

    Eric

  3. #93
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Not bad , if you post that code here, I'll add a link to it from my index page -- people shouldn't have to fish-out code.

  4. #94
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    String.prototype.reverse();


    This has been improved upon by Ultimater's script below.

    This prototype function exists for arrays but not strings... until now! This will reverse a string for example from "abcd" to "dcab". I can't see any use for it but I was outraged that it had support for Array but not for String .

    Code:
    String.prototype.reverse=function(){var t=this.length,s="";for(var i=0;i<t;i++)s+=this.substr(t-i-1,1);return s;}
    A simple test:

    PHP Code:
    document.write("I am a large unintelligible Moose".reverse());
    // Creates: "esooM elbigilletninu egral a ma I" 
    Last edited by BigMoosie; 05-22-2005 at 03:24 AM.

  5. #95
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    You may be familiar with Ultimater's Array.prototype.escape. Recently I read about a prototype function built into JavaScript called Array.prototype.map that makes it easy to apply a function to each element of an array. That lead me to nullify Ultimaters prototype with:

    myArray.map(escape);

    However upon testing this to my surprise it did not work! I realised that this map function works only in JavaScript 1.5 and is not well supported, so I wrote it!

    Array.prototype.map(callback[, thisObject]);

    For documentation on how to use this function visit this page.

    Code:
    if(!Array.prototype.map)Array.prototype.map=function(){var r=new Array(),t=this.length;for(var i=0;i<t;i++)r[i]=arguments[0].apply(this[i],[this[i],i,this]);return r;}
    Can somebody please verify that I have made the correct inference of the criteria of this function?
    Last edited by BigMoosie; 05-21-2005 at 08:23 AM.

  6. #96
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Array.prototype.map does not appear to be a part of ECMA 262 Specification.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #97
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Oh well, it might still come in handy.

    Number.prototype.factorise();


    • Takes no arguments, returns all of the prime factors in an array for example:

      (100).factorise() = [2, 2, 5, 5]

      or

      (806652).factorise() = [2, 2, 3, 3, 3, 7, 11, 97]

      .
    • Optimised to be quite effecient, will only cause the computer to freeze on rediculously huge primes.
    • Stores results in an array so subsequent requests for the same number wont have to be solved.
    • Trying to factorise a number other than a natural number will return NaN.


    PHP Code:
    Number.prototype.factorise=function(){
        if (
    parseInt(this)!=Math.abs(this) || this==0) return Number.NaN;
        var 
    stored=Number.prototype.factorise;
        if (
    stored[this]) return stored[this];
        var 
    factors=new Array();
        var 
    finish=Math.sqrt(this);
        var 
    current=this;
        var 
    testing=2;
        while (
    testing<=finish) {
            if (
    current%testing==0) {
                
    factors.push(testing);
                
    current/=testing;
                
    finish=Math.sqrt(current);
            }
            else 
    testing+=testing>4?2:1;
        }
        if (
    current!=1factors.push(current);
        
    stored[this]=factors;
        return 
    factors;
    }
    Number.prototype.factorise.stored=new Array(); 
    ---------------------------------------------------------------------------------------------------

    Some people may be using this function and wish to display display the result to the user in which case they should use:

    Array.prototype.powerise();

    Designed for use in conjuction with Number.prototype.factorise() but may be used elsewhere.

    • Takes no arguments and returns a string representing the values in the array using indices for example:

      [2, 2, 5, 5].powerise() = "2² + 5²"

      or

      (806652).factorise().powerise() = "2² + 3³ + 7 + 11 + 97"

      .
    • Array values can be anything including decimals or strings and still make sence to some analagy.
    • If not being used in conjunction with factorise() then the Array values should be sorted ( myArray.sort(); ) before entering this function.


    Code:
    Array.prototype.powerise=function(){str="",num=this[0],pow=1,len=this.length;if(!len)return "";for(var i=1;i<len;i++){if(this[i]==num)pow++;if(this[i]!=num){str+=num;if(pow-1)str+="<SUP>"+pow+"</SUP>";str+=" + ";num=this[i];pow=1;}}str+=num;if(pow>1)str+="<SUP>"+pow+"</SUP>";return str;}
    Last edited by BigMoosie; 05-21-2005 at 11:25 AM. Reason: Factorise and Powerise

  8. #98
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    A revision of BigMoosie's String.prototype.reverse function:
    Code:
    String.prototype.reverse=function(){
    return this.split("").reverse().join("")
    }

  9. #99
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Dammit, good point I didnt think of that .

  10. #100
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534

    Hcf

    Array.prototype.HCF();

    HCF = Highest Common Factor

    Ok, first of all your array mush be full of integers (positive or negative). The returning value will be the highest common factor of the values. So for example:

    [10,5,15].HCF(); // returns 5

    This will not modify your array unless you send an argument of true, then you will still get the same return value but each element will also be divided by that value. For example:

    var a=[10,5,15];
    a.HCF(true); //returns 5;
    document.write(a); // writes "2,1,3"


    Code:
    Array.prototype.HCF=function(){var v=new Array(),L=this.length,s=0;for (var i=0;i<L;i++){if(parseInt(this[i])!=this[i])return Number.NaN;v[i]=this[i];if(v[i]<v[s])s=i;}f=Math.sqrt(v[s]),t=2;for(i=0;i<L;i++)if(v[i]%v[s]!=0)break;if(i==L){if(arguments[0])for(i=0;i<L;i++)this[i]/=v[s];return v[s];}while(t<=f){for(i=0;i<L;i++)if(v[i]%t!=0)break;if (i==L){for(i=0;i<L;i++)v[i]/=t;f=Math.sqrt(v[s]);}else t+=t>4?2:1;}var r=this[s]/v[s];if(arguments[0])for(i=0;i<L;i++)this[i]=v[i];return r;}
    Great for simplifying fractions or factorising binomials etc.
    .
    Last edited by BigMoosie; 05-22-2005 at 09:27 AM.

  11. #101
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887

    Date.prototype.defaultView

    Ever need a Date prototype-function to display the date in this format?
    "07/23/1984"

    Code:
    <script type="text/javascript">
    
    Date.prototype.defaultView=function(){
    var dd=this.getDate();
    if(dd<10)dd='0'+dd;
    var mm=this.getMonth()+1;
    if(mm<10)mm='0'+mm;
    var yyyy=this.getFullYear();
    return String(mm+"\/"+dd+"\/"+yyyy)
    }
    
    </script>
    <script type="text/javascript">
    
    var today = new Date(); 
    alert('Today is '+today.defaultView()) 
    
    </script>

  12. #102
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Voting
    Since there have been several functions provided through-out this thread and it's becoming very hard to find that brilliant function your looking for,
    there has been added a new feature to allow you to vote for the function-links provided in my first post -- the index.
    For more details, see my first post.

    Enjoy the new feature

  13. #103
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887

    Date.prototype.toMMDDYYYYString

    Charles wrote my function in one line:
    Code:
    <script type="text/javascript">
    <!--
    Date.prototype.toMMDDYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' +  (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(),  this.getFullYear()].join('/')}
    
    alert (new Date().toMMDDYYYYString())
    // -->
    </script>
    Here's a link to his post:
    http://www.webdeveloper.com/forum/sh...d.php?p=376561

  14. #104
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    I'm bored .

    Code:
    String.prototype.dyslexia=function(){var a=this.length,b="";for(var i=0;i<a;i++)b+=Math.random()>0.5?this.substr(i,1).toUpperCase():this.substr(i,1).toLowerCase();return b;}

  15. #105
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    String.prototype.toPigLatin();


    This could probable be improved upon, I'm not even sure that what I believe to be pig latin is very widely accepted. How close am I?

    Code:
    String.prototype.toPigLatin=function(){
    	var a=this.split(" "), b=a.length, c, d, e;
    	for (var i=0; i<b; i++) {
    		if (a[i].length>1) {
    			d=a[i].length;
    			for (var j=0; j<d; j++){
    				c=a[i].substr(0,1);
    				if (c!="a" && c!="e" && c!="i" && c!="o" && c!="u") {
    					a[i]=a[i].substring(1,d)+c;
    				}
    			}
    			e=a[i].substr(d-1,1);
    			if (e=="a" || e=="e" || e=="i" || e=="o" || e=="u") a[i]+="way";
    			else a[i]+="ay";
    		}
    	}
    	return a.join(" ");
    }
    
    document.write("I like shooting frogs".toPigLatin());
    	// I ikelay ootingshay ogsfray

Thread Information

Users Browsing this Thread

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