ok and also sorry about that
Printable View
ok and also sorry about that
I blogged about a prototype for comparing colors and changing hex to rgb here.
http://radio.javaranch.com/pascarell...513366466.html
Eric
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.
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 :p .
A simple test: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;}
PHP Code:document.write("I am a large unintelligible Moose".reverse());
// Creates: "esooM elbigilletninu egral a ma I"
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.
Can somebody please verify that I have made the correct inference of the criteria of this function?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;}
Array.prototype.map does not appear to be a part of ECMA 262 Specification.
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!=1) factors.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;}
A revision of BigMoosie's String.prototype.reverse function:
Code:String.prototype.reverse=function(){
return this.split("").reverse().join("")
}
:mad: Dammit, good point I didnt think of that :cool: .
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"
Great for simplifying fractions or factorising binomials etc.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;}
.
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>
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 :)
Charles wrote my function in one line:
Here's a link to his post: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>
http://www.webdeveloper.com/forum/sh...d.php?p=376561
I'm bored :D.
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;}
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