Boolean XOR implementation in JavaScript

Can we implement Boolean XOR in Javascript ? Yes , we can implement ,As we know that  JavaScript provides a boolean AND operator (&&), a boolean OR operator (||), and a boolean NOT operator (!). But it is missing a boolean XOR operation. (In English, XOR can be stated as "If A is true or B is true, but not if both are true.") ,Before doing this we should have a little bit knowledge of javascript prototype . The following simple code adds an XOR() method to the Boolean object . 


Boolean.prototype.XOR=function(bool2){
   var bool1=this.valueOf();
   return (bool1==true && bool2==false) || (bool2==true && bool1==false);//return (bool1 && !bool2) || (bool2 && !bool1); 
}
true.XOR(false); //returns a value of true 

(The above method requires the passed value to be an actual boolean value to succeed. The second option, commented out, will attempt to cast bool2 to a boolean value for the comparison. If that line were used instead, values of 0, null'' and undefined would be interpretted as false, and other non-empty values such as 1 or "foo" will be interpretted as a value of true.)

Read the rest of this entry »

Number Formatting in Javascript

Number formatting is very typical process in every programming language , here ,I have a solution of this type of problem , which is written in javascript language , in these lines of code ,you can convert any type of number as you want like "1,056 items at $3.90 per item amounts to a total of $4,118.40" this type of number conversion , now question arise here that ,will it run on every language like ASP , JSP....etc ?
again answer is same yes always , before trying this I recommend that get a little bit knowledge of prototype of  javascript language it will help you to understand block of code. Click here to Download Demo .


Code:
  1. <!--Author : Mohammad Usman
  2.    Date   : 10/03/2011 -->
  3. <head><title>Number Formatting</title>
  4. <script language="javascript">
  5. function numberformat(){
  6. Number.prototype.toCurrency=function(noFractions,currencySymbol,decimalSeparator,thousandsSeparator){
  7.    var n,startAt,intLen;
  8.    if (currencySymbol==null) currencySymbol="$";
  9.    if (decimalSeparator==null) decimalSeparator=".";
  10.    if (thousandsSeparator==null) thousandsSeparator=",";
  11.    n = this.round(noFractions?0:2,true,decimalSeparator);
  12.    intLen=n.length-(noFractions?0:3);
  13.    if ((startAt=intLen%3)==0) startAt=3;
  14.    for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
  15.   return currencySymbol+n;
  16. }
  17. Number.prototype.toInteger=function(thousandsSeparator){
  18.   var n,startAt,intLen;
  19.   if (thousandsSeparator==null) thousandsSeparator=",";
  20.   n = this.round(0,true);
  21.   intLen=n.length;
  22.   if ((startAt=intLen%3)==0) startAt=3;
  23.   for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
  24.   return n;
  25. }
  26. Number.prototype.round=function(decimals,returnAsString,decimalSeparator){
  27.   //Supports 'negative' decimals, e.g. myNumber.round(-3) rounds to the nearest thousand
  28.   var n,factor,breakPoint,whole,frac;
  29.   if (!decimals) decimals=0;
  30.   factor=Math.pow(10,decimals);
  31.   n=(this.valueOf()+"");         //To get the internal value of an Object, use the valueOf() method
  32.   if (!returnAsString) return Math.round(n*factor)/factor;
  33.   if (!decimalSeparator) decimalSeparator=".";
  34.   if (n==0) return "0."+((factor+"").substr(1));
  35.   breakPoint=(n=Math.round(n*factor)+"").length-decimals;
  36.   whole = n.substr(0,breakPoint);
  37.   if (decimals>0){
  38.       frac = n.substr(breakPoint);
  39.       if (frac.length<decimals) frac=(Math.pow(10,decimals-frac.length)+"").substr(1)+frac;
  40.      return whole+decimalSeparator+frac;
  41.   }else return whole+((Math.pow(10,-decimals)+"").substr(1));
  42. }
  43. String.prototype.insertAt=function(loc,strChunk){
  44.   return (this.valueOf().substr(0,loc))+strChunk+(this.valueOf().substr(loc))
  45. }
  46. var quantity=1056;
  47. var costPer=3.9;
  48. var totalCost=quantity*costPer;
  49. alert(quantity.toInteger()+" items at "+costPer.toCurrency()+" per item amounts to a total of "+totalCost.toCurrency());
  50. //Yields "1,056 items at $3.90 per item amounts to a total of $4,118.40"
  51. }
  52. </head>
  53. <input type="button" value="Example of Number Format" onclick="numberformat();"/>
  54. </html>


Read the rest of this entry »

Date Formatting in Javascript

Can we format date in javascript as per our requirement ? Yes, we can do this in few lines of code in javascript , Outputting a lot of dates to the browser, and want to format them nicely? The following code adds a customFormat() method to the Date class which allows you to have a Date object turn into a string formatted just the way you like. Simply include this small block of code (in a separate client-side library file, directly in your JS ) and all date objects will suddenly be able to cleanly format themselves. Click here to Download Demo .

Code :
  1. <!--Author : Mohammad Usman
  2.    Date   : 10/01/2011 -->
  3. <head><title>Date Formatting in Javascript</title>
  4. <script language="javascript">
  5. function datetestmethode(){
  6. Date.prototype.customFormat=function(formatString){
  7.    var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,dMod,th;
  8.    YY = ((YYYY=this.getFullYear())+"").substr(2,2);
  9.    MM = (M=this.getMonth()+1)<10?('0'+M):M;
  10.   MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substr(0,3);
  11.   DD = (D=this.getDate())<10?('0'+D):D;
  12.   DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substr(0,3);
  13.   th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
  14.    formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
  15.    h=(hhh=this.getHours());
  16.    if (h==0) h=24;
  17.    if (h>12) h-=12;
  18.    hh = h<10?('0'+h):h;
  19.   ampm=hhh<12?'am':'pm';
  20.   mm=(m=this.getMinutes())<10?('0'+m):m;
  21.   ss=(s=this.getSeconds())<10?('0'+s):s;
  22.   return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm);
  23. }
  24. var now=new Date();
  25. alert("Today is "+now.customFormat('#DDDD#, #MMMM# #D##th#,#YYYY#')+"\nThe time is "+now.customFormat('#h#:#mm##ampm#')+".");
  26. }
  27. </head>
  28. <input type="button" value="Click Me to Format Date" onclick="datetestmethode();"/>
  29. </html>


Read the rest of this entry »

Comment On Facebook