I had written the follwing code with multiple if statements and repetitive expressions.
this code works in order.
I tired to convert the same to switch () and case statments insteas of if;Code:<head> <script type="text/javascript"> <!-- function checkform (BP) { // ** START ** if (isNaN (document.BP.basic_pension.value) || (document.BP.basic_pension.value ==="" )) { alert("entered wrong value NaN(NOT a Number)\n" +(document.BP.basic_pension.value)) ; document.BP.basic_pension.focus(); return false ; } // ** END ** return true ; } //--> </script> <script type="text/javascript"> function calculate(BP) { var bp = parseInt(document.BP.basic_pension.value) var pp=(2/3)*bp var drm= document.BP.DRM.value //alert (pp) var dor1=document.BP.dor.value var da1=document.BP.DA_DR.value gp=document.BP.GPension.value gp=pp+(bp*da1) var dor2 = new Date(dor1) /*alert(dor1) alert(dor2)*/ if (dor2>=new Date('11/1/2007')) { da1=0.702 document.BP.DA_DR.value=da1 drm=(bp*da1) gp = pp+(bp * da1) document.BP.GPension.value =Math.round(gp) document.BP.PP.value=Math.round(pp) document.BP.DRM.value=Math.round(drm) /*alert("da2"+da1) alert("gp" + gp+ "bp" +bp+ "pp"+ pp)*/ } else if (dor2>=new Date('11/1/2002')&& (dor2<=new Date('10/31/2007'))){ da1=1.089 document.BP.DA_DR.value=da1 drm=(bp*da1) gp = pp+(bp * da1) document.BP.GPension.value =Math.round(gp) document.BP.DRM.value=Math.round(drm) document.BP.PP.value=Math.round(pp) } else if (dor2>=new Date('11/1/1997')&& (dor2<=new Date('10/31/2002'))){ da1=1.8144 document.BP.DA_DR.value=da1 gp = pp+(bp * da1) drm= bp * da1 document.BP.GPension.value =Math.round(gp) document.BP.DRM.value=Math.round(drm) document.BP.PP.value=Math.round(pp) //alert("da4" + da1) } else if (dor2>=new Date('11/1/1992')&& (dor2<=new Date('10/31/1997'))) { da1=3.1150 document.BP.DA_DR.value=da1 /*alert("da5" + da1)*/ gp = pp+(bp * da1) drm= bp * da1 document.BP.GPension.value =Math.round(gp) document.BP.DRM.value=Math.round(drm) document.BP.PP.value=Math.round(pp) } else if (dor2>=new Date('11/1/1987')&& (dor2<=new Date('10/31/1992'))) { da1=6.8809 document.BP.DA_DR.value=da1 /*alert("da6"+da1)*/ gp = pp+(bp * da1) drm= bp * da1 document.BP.GPension.value =Math.round(gp) document.BP.DRM.value=Math.round(drm) document.BP.PP.value=Math.round(pp) } else{ alert(" Your date of Retirement must be in 'mm/dd/yyyy' format &\n Basic Pension = A Number! \n Donot leave the fields blank! Retry!") } } </script> </head> <body> <form name="BP" id="BP" > <tr> <th width="193" height="28" scope="row"> <table width="197" border="1" bordercolor="#0066FF" cellpadding="1" cellspacing="1" style="background-color:#FFFF66"> <caption> </caption> <tr> <th height="24" colspan="2" scope="row">PENSION CALCULATOR<br /> for <br /> RBI Pensioners </th> </tr> <tr> <th width="123" height="24" scope="row">Basic Pension</th> <td width="61" > <input name = "basic_pension" type = "text" id="basic_pension" style="background-color:#F9C" size = "10" maxlength="6" value= "0" onchange= "checkform(BP)" /> </span></td> </tr> <tr> <th style="background-color:#C6C>" scope="row">Date of Retirement</th> <td > <input name = "dor" type = "text" id="dor2" value ="" size = "10" style="background-color:#F9C"/> </td> </tr> <tr> <th scope="row">DA/DR</th> <td><samp> <input type = "text" name = "DA_DR" size = "10" value ="0" readonly="readonly"/> </samp></td> </tr> <tr> <th scope="row">Pension payable</th> <td><samp> <input type = "text" name = "PP" size = "10" value ="" readonly="readonly" /> </samp></td> </tr> <tr> <th scope="row">DR admissible</th> <td><samp> <input type = "text" name = "DRM" size = "10" value ="" readonly="readonly" /> </samp></td> </tr> <tr> <th scope="row">Gross Pension</th> <td><input type = "text" name = "GPension" size = "10" value = "" readonly="readonly" /></td> </tr> <tr> <th colspan="2" scope="row"><input type = "button" name = "b1" value = "Calculate Pension" onclick = "calculate(BP)";/></th> </tr> </table> </th> </tr> </form> </table> </body> </html> } }
I wrote the following .
Now whenever I run the switch code it jumps to default: whatever date I enter in the 'date of retirement.and omits all the case statements even if the date entered in calculator belongs to other than default.It calculates what is just above the switch statement.Code:function calculate(BP) { var bp = parseInt(document.BP.basic_pension.value) var pp=(2/3)*bp var drm= document.BP.DRM.value //alert (pp) var dor1=document.BP.dor.value alert(dor1) var da1=document.BP.DA_DR.value gp=document.BP.GPension.value gp=pp+(bp*da1) var dor2 = new Date(dor1) switch(dor2) { case(dor2)>= new Date('11/1/2007'): da1=0.702 document.BP.DA_DR.value=da1 drm=(bp*da1) gp = pp+(bp * da1) document.BP.GPension.value =Math.round(gp) document.BP.PP.value=Math.round(pp) document.BP.DRM.value=Math.round(drm) break; case (dor2>=new Date('11/1/2002')&& (dor2<=new Date('10/31/2007'))): da1=1.089 document.BP.DA_DR.value=da1 break; case (dor2>=new Date('11/1/1997')&& (dor2<=new Date('10/31/2002'))): da1=1.814 document.BP.DA_DR.value=da1 break; case (dor2>=new Date('11/1/1992')&& (dor2<=new Date('10/31/1997'))): da1=3.1150 document.BP.DA_DR.value=da1 break; case(dor2>=new Date('11/1/1987')&& (dor2<=new Date('10/31/1992'))): da1=6.8809 document.BP.DA_DR.value=da1 break; default:{ alert("abc Your date of Retirement must be in 'mm/dd/yyyy' format &\n Basic Pension = A Number! \n Donot leave the fields blank! Retry!") } drm=(bp*da1) gp = pp+(bp * da1) document.BP.GPension.value =Math.round(gp) document.BP.PP.value=Math.round(pp) document.BP.DRM.value=Math.round(drm) /*alert("da2"+da1) alert("gp" + gp+ "bp" +bp+ "pp"+ pp)*/ the original code with if statements which works <script type="text/javascript">
As a learner I wish to know where I have missed the steps?
and whether my if code can be converted to Switch ?
and last how can I write a more concise code even with if in case switch is not working?
thanks
regards
vkwd7


Reply With Quote
Bookmarks