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 .
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.
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
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();
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"
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.
Bookmarks