www.webdeveloper.com
Page 11 of 15 FirstFirst ... 910111213 ... LastLast
Results 151 to 165 of 221

Thread: Here are 2 prototype-functions I created.

  1. #151
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Your way of figuring out the fraction is definitely better. I just pulled that other one out of my @$$, although it's aiming for the same "something/999" kind of thing.

    Here's what I mean about marking up the string. I tell the function which part of the decimal repeats by putting it in square brackets
    Code:
    String.prototype.toRational=function() {
      var a=this.substring(0, this.indexOf("."));
      var b=this.substring(this.indexOf(".")+1, this.indexOf("["));
      var c=this.substring(this.indexOf("[")+1, this.indexOf("]"));
      
      var lena=a.length;
      var lenb=b.length;
      var lenc=c.length;
    
      var x=parseInt(a+b);
      var y=parseInt(a+b+c);
      
      var z=y-x;
      var den="";
      for(var i=0; i<lenc; i++) den+="9";
      for(var i=0; i<lenb; i++) den+="0";
    
      return z+"/"+den+"="+z/parseInt(den);
    }
    document.write("5.1[42857]".toRational());
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  2. #152
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    A number of useful functions:
    Code:
    <script type="text/javascript">
    Date.prototype.getShortDayOfWeek=function(){
    return ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][this.getDay()]
    }
    Date.prototype.getShortMonth=function(){
    return ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][this.getMonth()]
    }
    Date.prototype.toArmyTime=function(){
    var hh=this.getHours();if(hh<10)hh="0"+hh;
    var mm=this.getMinutes();if(mm<10)mm="0"+mm;
    var ss=this.getSeconds();if(ss<10)ss="0"+ss;
    return hh+":"+mm+":"+ss
    }
    Date.prototype.getDateFormat1=function(){
    return this.getShortDayOfWeek()+" "+this.getShortMonth()+" "
    +this.getDay()+" "+this.toArmyTime()+" "+this.getFullYear()
    }
    
    </script>
    <script type="text/javascript">
    
    var today = new Date(); 
    alert(today.getDateFormat1())
    
    </script>

  3. #153
    Join Date
    Jul 2004
    Location
    web develepment land
    Posts
    164
    Quote Originally Posted by Ultimater
    HTML Code:
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    String.prototype.splitAt=function(){return [this.substr(0,arguments[0]),this.substr(arguments[0])]}
    </SCRIPT>
    The above defines the prototype-function splitAt.

    The following is an example of the function in use:
    HTML Code:
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    alert("abc".splitAt(2)[0])//alerts ab
    alert("abc".splitAt(2)[1])//alerts c
    </SCRIPT>
    The way the function works:
    The function always returns an Array with a length of 2 elements.
    The given argument is the maxium string-length to allow for the the first element.
    The second element is the remaining string that gets cut-off by the first element.
    another way you can do this is:
    HTML Code:
    String.prototype.splitAt=function(){return this.split(this.charAt(arguments[0]))}
    Last edited by emblem; 06-20-2005 at 02:17 PM.
    Need help with any of my scripts? Come chat with me!
    Javascript And File System!
    EMBLEM
    |-____/\____---------------|
    |-\__/||\__/--TRUST--------|
    |--\/|--|\/----THE---------|
    |--/______\-----EMBLEM-----|
    |-----\/-------------------|

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

  5. #155
    Join Date
    Jul 2004
    Location
    web develepment land
    Posts
    164
    Quote Originally Posted by Ultimater
    Edit your post:
    }
    nice call thanx
    Need help with any of my scripts? Come chat with me!
    Javascript And File System!
    EMBLEM
    |-____/\____---------------|
    |-\__/||\__/--TRUST--------|
    |--\/|--|\/----THE---------|
    |--/______\-----EMBLEM-----|
    |-----\/-------------------|

  6. #156
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    It doesn't replace my function for the same purpose.
    Mind showing us how you intended to use yours?
    Last edited by Ultimater; 06-20-2005 at 02:34 PM.

  7. #157
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Oh, I see how yours works now.
    You are losing the character that you split at.
    Also, yours is able to return more than two array-elements.

    Code:
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript"> 
    String.prototype.splitAt_Ultimater=function(){return [this.substr(0,arguments[0]),this.substr(arguments[0])]} 
    String.prototype.splitAt_emblem=function(){return this.split(this.charAt(arguments[0]))}
    </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript"> 
    alert("abcabcabcabc".splitAt_Ultimater(3).length)
    alert("abcabcabcabc".splitAt_emblem(3).length)
    </SCRIPT>
    Last edited by Ultimater; 06-20-2005 at 02:42 PM.

  8. #158
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    It looks like when you made your splitAt function, you were thinging of the split function.
    When I made my function, I was think about how to split a string into two parts and return the two w/o losing any part of the string.

  9. #159
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    This does the same job as Math.abs(n) but is about 30% faster.

    Code:
    Number.prototype.abs=function(){return this>0?this:-this;}

  10. #160
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Array.toSource();

    I needed to send a multi dimensional array to function through setTimeout, however this lost the internal structure of it so I created this that would turn it into a string that calling eval() would reconstruct it, and setTimeout is kind of like eval():

    Code:
    Array.prototype.toSource=function(){
    	var L=this.length,s='[',t,i;
    	for (i=0; i<L; i++) {
    		if (i>0) s+=',';
    		if (this[i].constructor==Array) s+=this[i].toSource();
    		else switch (typeof this[i]) {
    			case "number" : s+=this[i]; break;
    			case "boolean" : s+=this[i]; break;
    			default : s+='"'+this[i].toString().split('"').join('\\"')+'"';
    		}
    	}
    	return s+']';
    }
    Obviously object references will be lost.
    Last edited by BigMoosie; 07-03-2005 at 02:16 AM.

  11. #161
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    If you dont mind losing the .toString() method of Arrays, you can use this, it does exactly the same as toSource() above ^ but is alot shorter.

    Code:
    Array.prototype.toString=function(){return '['+this.join(", ")+']';}

  12. #162
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Here is one to grab a query string value. I am sure it could be done differently, but I was having fun with string manipulation and such.

    Code:
    <script type="text/javascript">
    
      String.prototype.GetQSValue = function(){
        if(!arguments[0])return null;
        var strFL = unescape(this)
        var regEx = new RegExp("[?&]" + arguments[0] + "=.*$","g");
        var strMatch = strFL.match(regEx);
        if(strMatch == null)return null
        var arrParts = strMatch.toString().split("&");
        return (arrParts[0].length==0)?arrParts[1].split("=")[1]:arrParts[0].split("=")[1];
      }
    
      var strLink = window.location.search;
      var strId = strLink.GetQSValue('id');
      alert(strId); 
    
    </script>
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  13. #163
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Array.prototype.binarySearch(term);

    • Will return the index of a value in an array.
    • If not found will return -1.
    • Requires that the array be sorted before-hand.
    • If there is more than one isntance it will not necasserily return the first one.
    • Despite these drawbacks it is unbeleivably fast when using long arrays.


    Code:
    Array.prototype.binarySearch=function(q){
    	var mid, lower=0, upper=this.length;
    	while (lower<=upper) {
    		mid=Math.floor((lower+upper)/2);
    		if (q==this[mid]) return mid;
    		if (q<this[mid]) upper=mid-1;
    		else lower=mid+1
    	}
    	return -1;
    }

  14. #164
    Join Date
    Nov 2002
    Location
    England
    Posts
    693
    String Encryption. I didn't write the algorithm or the source, I just adapted it into a prototype function.
    Attached Files Attached Files

  15. #165
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Is there a decyrption function to go with it?

Thread Information

Users Browsing this Thread

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