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
Printable View
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
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.
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 beThat would allow you to specify how many of the characters you want to replace instead of always 1.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)
}
There you go, i couldn't have said it better myself.
The above defines the prototype-function splitAt.HTML Code:<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
String.prototype.splitAt=function(){return [this.substr(0,arguments[0]),this.substr(arguments[0])]}
</SCRIPT>
The following is an example of the function in use:
The way the function works:HTML Code:<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
alert("abc".splitAt(2)[0])//alerts ab
alert("abc".splitAt(2)[1])//alerts c
</SCRIPT>
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.
The above returns the first n elements of an array.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>
example:
Code:alert([0,1,2,3].firstElements(2).join(" "))//alerts 0 1
isn't there already a function Array.slice() that can do that?
hmm.... I totally forgot about the slice function...
It's just simply not part of my function-vocabulary yet. :D
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>
This function is very useful. It merges together duplicate data inside of an array:
Note: that the function doesn't work with multidimensional arrays.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>
If you need to call the escape or unescape function on each element of an array:
note: doesn't work with multidimensional arrays.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>
My solution does the job in half the time:Quote:
Originally Posted by Ultimater
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;}
Heck even the seemingly irrelivent change I just made cuts down on my previous time by about 15%
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.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;}
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).
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.
DescriptionCode: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]);}
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.
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;
}