/    Sign up×
Community /Pin to ProfileBookmark

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
//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>&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>
<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>

}
}
[/CODE]

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
//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”>

[/CODE]

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 12.2012 — Case does not evaluate to a logical condition.

Before the switch, create a variable to match the conditions desired for the dates, ie:
<i>
</i>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;
}
×

Success!

Help @vkwd7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...