Thread: converting if to Switch not working!

1. Member
Join Date
Oct 2012
Location
india
Posts
12

converting if to Switch not working!

I had written the follwing code with multiple if statements and repetitive expressions.
this code works in order.

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
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)
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("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)

}
else if
(dor2>=new Date('11/1/1992')&& (dor2<=new Date('10/31/1997')))
{
da1=3.1150
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)
}

else if
(dor2>=new Date('11/1/1987')&& (dor2<=new Date('10/31/1992')))
{
da1=6.8809
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)

}
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>

<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>&nbsp;
</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&gt;" 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>
<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 tired to convert the same to switch () and case statments insteas of if;
I wrote the following .
Code:
``` function calculate(BP) {
var bp = parseInt(document.BP.basic_pension.value)
var pp=(2/3)*bp
var drm= document.BP.DRM.value
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)

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("gp" + gp+ "bp" +bp+ "pp"+ pp)*/

the original code with if statements which works

<script type="text/javascript">```
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.
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

2. Case does not evaluate to a logical condition.

Before the switch, create a variable to match the conditions desired for the dates, ie:
Code:
```var caseTest = 0;  // default condition
if ( ... date logic for condition 1 ...) caseTest = 1;
if ( ... date logic for condition 2 ...) caseTest = 2;
// etc.
switch (caseTest) {
case 1: ...xxx...; break
case 2: ...yyy...; break
default: ...zzz...; break;
}```

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

X vBulletin 4.2.2 Debug Information

• Page Generation 0.26867 seconds
• Memory Usage 2,856KB
• Queries Executed 13 (?)
Template Usage (33):
• (3)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (2)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (2)postbit
• (2)postbit_onlinestatus
• (2)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete