www.webdeveloper.com
Page 6 of 15 FirstFirst ... 45678 ... LastLast
Results 76 to 90 of 221

Thread: Here are 2 prototype-functions I created.

  1. #76
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Jona
    IE’s scripting engine has a tendency to be more snappy than Firefox’s (by the way, the preferred abbreviation for Firefox is “Fx” not “FF”).
    My preffered abbreviation of it is FF... but I guess if it conveys the incorrect message to some I should use Fx. Thanks

  2. #77
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Personally, I’m not against the use of FF, but in order to have a consistent meaning, a word must always have the same abbreviation, don’t you agree? That’s my thoughts on it, anyway–kind of like the same reason why we have standardized HTML.

    Let’s not get off topic, though. I don’t think Ultimater would appreciate that.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #78
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    That discussion seems awfully familiar, much like the one between NN and NS.
    I prefer NS over NN and I prefer FF over Fx.

    Anyways, BigMoosie, How is the variable t1 involved here?
    Quote Originally Posted by BigMoosie
    Code:
    Function.prototype.time = function() {
    	var t1 = new Date();
    	this.apply(null,arguments);
    	var t2 = new Date();
    	var t3 = new Date();
    	return 2*t2 - t3 - t2;
    };
    Would you mind rewriting the function so I can provide a clean link to it from my index in the first post?

  4. #79
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    return 2*t2 - t3 - t2;
    Oops, that should have been:

    Code:
    return 2*t2 - t3 - t1;
    Like the mathematics above it stated. Sorry for any confusion, I have made the edit.

  5. #80
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534

    String.dex(string, number);

    String.prototype.dex(string, number);

    For me personally, this is the most useful prototype function in this thread.


    Often I will want to know the position of... say the third occurance of a string within another. This is similar to indexOf() but instead of just getting the first occurance you can specifiy a second argument which will be the nth occurance.

    • Specifying a negative value for n will count from the back rather than the front.
    • Specifying no value for n will presume the value 1 for n.
    • Specifying a string or decimal or zero for n will return -1.
    • If the string is not found at the nth occurance will also return -1.


    Code:
    String.prototype.dex=function(){var f=arguments[1],L=arguments[0].length,c=0;if(f==undefined)f=1;else if(parseInt(f)!=f||f==0)return -1;if(f>0){for(var i=0;i<f;i++){c=this.indexOf(arguments[0],c+i*L);if(c<0)return -1;}return c;}if(f<0){c=this.length;for(var i=f;i<0;i++){c=this.lastIndexOf(arguments[0],c-L);if(c<0)return -1;}return c;}}
    An example of how to use this:

    Code:
    var str="00aaaa00aa00";
    
    alert(str.dex("aa", 1)) // alerts 2
    alert(str.dex("aa", 2)) // alerts 4
    alert(str.dex("aa", 3)) // alerts 8
    alert(str.dex("aa", 4)) // alerts -1 because there is no fourth occurance
    
    alert(str.dex("aa", -1)) // alerts 8
    alert(str.dex("aa", -2)) // alerts 4
    alert(str.dex("aa", -3)) // alerts 2
    alert(str.dex("aa", -4)) // alerts -1 because there is no fourth occurance
    
    alert(str.dex("aa", 0)) // alerts -1 because of invalid input 
    alert(str.dex("aa", "a")) // alerts -1 because of invalid input
    Last edited by BigMoosie; 05-21-2005 at 07:24 AM.

  6. #81
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Ok, I added both of those.
    Your String.prototype.dex function is alot like my String.prototype.indexAt function in my sig.
    Last edited by Ultimater; 05-15-2005 at 01:23 AM.

  7. #82
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Doh! For some reason that page only half loads, does this happen to you also?

    And if you made it why didnt you add it here already?

  8. #83
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Hmm... I might as well add it to the thread
    Code:
    String.prototype.indexAt=function(){
    var position=-1
    for(i=0;i<arguments[1];i++){
    position=this.indexOf(arguments[0],position+1)
    if(position==-1)return -1
    }
    return position
    }
    Example:
    Code:
    var myString="blah...blah;somemoretext_blah_blah"
    alert(myString.indexAt("blah",3))//returns 25

  9. #84
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Yes that page loads in IE but half in Fx. There is a flaw in your .pl file.

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

  11. #86
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Yep its fixed. I also made a fix to String.dex(), didnt realise till i saw yours that indexOf() took 2 arguments. My version drains less resources from the computer as it only calls indexOf() as many times as it has to.

  12. #87
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Number.prototype.factorial();

    For Those who dont know what factorial is it is the number of sequences that can exist with a set of items, derived by multiplying the number of items by the next lowest number until 1 is reached. For example, three items have six sequences (3x2x1=6): 123, 132, 231, 213, 312 and 321. Also note that zero factorial (in Maths noted by 0!) is equal to 1.

    This code is remarkably less laggy than traditional scripts for two reasons. If the input is greater than 170 then JavaScript will naturally overflow the value to infinity, instead of trying that this script will just presume that from the start. Also, once a value has been recognised, any factorial value below that is indexed in an array so subsequent calls will drain much less resources. I challenge anybody to beat this script in efficiency.

    Code:
    Number.prototype.factorial=function(){if(parseInt(this)!=Math.abs(this))return Number.NaN;if(this>170)return Number.POSITIVE_INFINITY;var d=Number.prototype.factorial.k;if(d[this])return d[this];w=d.length,c=d[w-1];for(var i=w;i<=this;i++){c*=i;d[i]=c;}return c;};Number.prototype.factorial.k=new Array(1,1);
    An example of how to use this:

    PHP Code:
    var num=3;
       
    alert(num.factorial()); // alerts 6;
    num=7;
       
    alert(num.factorial()); // alrts 5040;
    num=200;
       
    alert(num.factorial()); // alerts *Infinity*; 
    Last edited by BigMoosie; 05-21-2005 at 07:24 AM.

  13. #88
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    I guess the easiest way to beat that script for efficiency would be to simply store the factorials for all numbers up to 170 in a hard-coded array from the very beginning and return the appropriate NaN or Infinity otherwise. You'd never spend any time computing values, or checking for their existence in the array, since you already have them all.

    Of course, that is a pretty lame approach...
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  14. #89
    Join Date
    Jul 2004
    Location
    web develepment land
    Posts
    164
    Im not shure if theis goes here so tell me if it doesn't
    ProtoType Function: replaceAll
    Code:
    String.prototype.replaceAll=function() {return this.split(arguments[0]).join(arguments[1])}
    Example usage:
    Code:
    String.prototype.replaceAll=function() {return this.split(arguments[0]).join(arguments[1])}
    test="It Did Not Work"
    test=test.replaceAll(" Not","")
    alert(test)
    Last edited by emblem; 05-19-2005 at 11:41 AM.
    Need help with any of my scripts? Come chat with me!
    Javascript And File System!
    EMBLEM
    |-____/\____---------------|
    |-\__/||\__/--TRUST--------|
    |--\/|--|\/----THE---------|
    |--/______\-----EMBLEM-----|
    |-----\/-------------------|

  15. #90
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Edit the error in your post and I'll add a link to your entry on the index in my first post.
    Code:
    alert(test)

Thread Information

Users Browsing this Thread

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