www.webdeveloper.com
Page 3 of 15 FirstFirst 1234513 ... LastLast
Results 31 to 45 of 221

Thread: Here are 2 prototype-functions I created.

  1. #31
    Join Date
    Mar 2005
    Location
    fl
    Posts
    17
    Hi, can you explain me for what can i use the 2 functions you made. Sorry but i'm new to javascript, also can you people please help me to validate my email form. Thanks

  2. #32
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    76
    Dont worry soyunlocofl, i also sort of a beginner (Im only 13).

    I belive what those functions are for is to duplicate an amout of text/code how many times you want, correct me if im wrong.

  3. #33
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    The first function replaces the nth character in a string with another string
    The second one creates a string that is n copies of the input.

    A useful extension to the first function might be
    Code:
    String.prototype.replaceAt=function() {
      var rlen=(arguments[2]==null?1:arguments[2]);
      return this.substring(0,arguments[0])+arguments[1]+this.substring(arguments[0]+rlen)
    }
    That would allow you to specify how many of the characters you want to replace instead of always 1.
    Last edited by HaganeNoKokoro; 04-10-2005 at 03:04 AM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  4. #34
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    76
    There you go, i couldn't have said it better myself.

  5. #35
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    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.

  6. #36
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    HTML Code:
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    Array.prototype.firstElements=function(){var temp=[];var total_length=arguments[0];if(total_length>this.length)total_length=this.length;for(var i=0;i<total_length;i++){temp[temp.length]=this[i]}return temp}
    </SCRIPT>
    The above returns the first n elements of an array.
    example:
    Code:
    alert([0,1,2,3].firstElements(2).join(" "))//alerts 0 1

  7. #37
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    isn't there already a function Array.slice() that can do that?
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  8. #38
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    hmm.... I totally forgot about the slice function...
    It's just simply not part of my function-vocabulary yet.

  9. #39
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    I came across the need of creating this one when I wrote it for my guest book:
    Code:
    <SCRIPT TYPE="TEXT/JAVASCRIPT" LANGUAGE="JAVASCRIPT"><!--
    function twodigits(){
    if(arguments[0]<10)return "0"+arguments[0]
    else return arguments[0]
    }
    Date.prototype.toMyLocalTime=function(){
    var WeekDay=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    var Month=["January","February","March","April","May","June","July","August","September","October","November","December"]
    var A_Hour=[12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11]
    return WeekDay[this.getDay()]+", "+Month[this.getMonth()]+" "+this.getDate()+", "+this.getYear()+
    " at "+A_Hour[this.getHours()]+":"+twodigits(this.getMinutes())+":"+twodigits(this.getSeconds())+
    ((this.getHours()<12)?" am":" pm")
    }
    //--></SCRIPT>
    <SCRIPT TYPE="TEXT/JAVASCRIPT" LANGUAGE="JAVASCRIPT"><!--
    document.write(new Date(1108688084*1000).toMyLocalTime());
    //--></SCRIPT>

  10. #40
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    This function is very useful. It merges together duplicate data inside of an array:
    Code:
    <script type="text/javascript">
    Array.prototype.mergeData=function(){var temp=this.sort();var DaLength=0;for(var i=0;i<this.length;i++){if((((i+1)in temp)&&temp[i]==temp[i+1])||(((i-1)in temp)&&temp[i]==temp[i-1]))delete temp[i];else DaLength++}temp=temp.sort();return temp.slice(0,DaLength)}
    </script>
    <script type="text/javascript">
    alert([0,2,0,3,2,3,4,1,-3,"a","b","a"].mergeData())
    </script>
    Note: that the function doesn't work with multidimensional arrays.

  11. #41
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    If you need to call the escape or unescape function on each element of an array:
    Code:
    <script type="text/javascript">
    Array.prototype.escape=function(){var temp=[];for(var i in this)temp[i]=escape(this[i]);return temp}
    Array.prototype.unescape=function(){var temp=[];for(var i in this)temp[i]=unescape(this[i]);return temp}
    </script>
    <script type="text/javascript">
    alert(["<",">"].escape().join(""))
    alert(["%3C","%3E"].unescape().join(""))
    </script>
    note: doesn't work with multidimensional arrays.

  12. #42
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Ultimater
    This function is very useful. It merges together duplicate data inside of an array:
    Code:
    <script type="text/javascript">
    Array.prototype.mergeData=function(){var temp=this.sort();var DaLength=0;for(var i=0;i<this.length;i++){if((((i+1)in temp)&&temp[i]==temp[i+1])||(((i-1)in temp)&&temp[i]==temp[i-1]))delete temp[i];else DaLength++}temp=temp.sort();return temp.slice(0,DaLength)}
    </script>
    <script type="text/javascript">
    alert([0,2,0,3,2,3,4,1,-3,"a","b","a"].mergeData())
    </script>
    Note: that the function doesn't work with multidimensional arrays.
    My solution does the job in half the time:

    Code:
    Array.prototype.mergeData=function(){var inTemp=this.sort();var outTemp=new Array();for (var i=0; i<this.length; i++){if (outTemp[outTemp.length-1]!=inTemp[i]) outTemp[outTemp.length]=inTemp[i];}return outTemp;}

  13. #43
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534

    Arrow

    Heck even the seemingly irrelivent change I just made cuts down on my previous time by about 15%


    Code:
    Array.prototype.mergeData=function(){var inTemp=this.sort();var inAt=this.length;var outTemp=new Array();var outAt=-1;for (var i=0; i<inAt; i++){if (outTemp[outAt]!=inTemp[i]){outTemp[outAt+1]=inTemp[i];	outAt++;}}return outTemp;}
    When making these functions it is a good idea to test them with a very large array (like 100,000 in length, but be sure not to freeze your computer!). But to track how long it takes, take the time before and after and subtract the two.

    Making changes like the ones I did make it more efficient. Instead of continually checking array lengths it has a variable to store that value and a it is incremented each time that the array is added to so it doesnt have to. Making lots of changes like this and adding more variables often increases efficency (one might at first expect the opposite).
    Last edited by BigMoosie; 05-10-2005 at 09:03 AM.

  14. #44
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    An improvement upon the substring method:

    Note! I origionally called this String.sub() but later realised that there already is a prototype called sub designed to return the string as a subscript.

    Code:
    String.prototype.subs=function(){return this.substring((arguments[0]<0)?this.length+arguments[0]:arguments[0],(arguments[1]<0)?this.length+arguments[1]:arguments[1]);}
    Description

    Often I will perform something like the following in my code:

    myString=myString.subString(3, myString.length-3);

    Now you can do just the same by calling:

    myString=myString.subs(3, -3);

    You see, a negative value is the same as counting from the end backwards, the way substring should have been in the first place.
    Last edited by BigMoosie; 05-13-2005 at 05:59 AM.

  15. #45
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    An improvement upon Jona's multi-dimensional array searching script.

    This modification allows for multiple data-types.

    Code:
    Array.prototype.search = function(s,q){
    	var len = this.length;
    	if (q) {
    		s=s.toString();
    		for(var i=0; i<len; i++){
    			if(this[i].constructor == Array) if(this[i].search(s,q)) return true;
    			if(this[i].toString().indexOf(s) != -1) return true;
    		}
    	}
    	else {
    		for(var i=0; i<len; i++){
    			if(this[i].constructor == Array) if(this[i].search(s,q)) return true;
    			if(this[i]===s) return true;
    		}
    	}
    	return false;
    }

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