Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation using switch case on radio Button

Status
Not open for further replies.

Cap2010

Programmer
Mar 29, 2000
196
CA
I want calculate using <b>switch statement</b> unable to do it. What could be the error. <br>Tried using if condition and for loop doesn't work. <br><br><b>Want to calculate each location, position and display the total with respective location and position at the bottom <br>What could be error and how to call all function in one function. </b><br> <br>function calc1() { <br> function loc1()<br> function loc2()<br>} <br><br><br>&lt;HTML&gt;&lt;HEAD&gt;<br>&lt;SCRIPT language=&quot;JavaScript&quot;&gt;<br>&lt;!-- <br> function position(pos){<br> var t=0;<br> switch(pos)<br> {<br> case 10:{t=10; break; }<br> case 9: {t=9; break; }<br> case 8: {t=8; break; }<br> case 7: {t=7; break; }<br> case 6: {t=6; break; }<br> case 5: {t=5; break; }<br> case 4: {t=4; break; }<br> case 3: {t=3; break; }<br> <br> default:<br> alert(&quot;You Must Select&quot;); break;<br> }<br>}<br><br> function location(loc){<br> var t1;<br> switch(loc)<br> {<br> case 10:{t1=10; break; }<br> case 9: {t1=9;&nbsp;&nbsp;break; }<br> case 8: {t1=8;&nbsp;&nbsp;break; }<br> case 7: {t1=7;&nbsp;&nbsp;break; }<br> case 6: {t1=6;&nbsp;&nbsp;break; }<br> case 5: {t1=5; break; }<br> case 4: {t1=4; break; }<br> case 3: {t1=3; break; }<br> <br> default:<br> alert(&quot;You Must Select&quot;); break;<br> }<br>}<br><br>function calc(testform){<br> var ttotal1;<br> call function position(pos);<br> {ttotal1 = parseInt(t); }<br> function location(loc);<br> {ttotal1&nbsp;&nbsp;= ttotal1 + parseInt(t1);}<br>}<br>&lt;/script&gt;<br><br>&lt;/HEAD&gt;<br><br>&lt;BODY BGCOLOR=&quot;#FFFFFF&quot;&gt;<br><br>&lt;form name=&quot;testform&quot; &gt; <br><br>&lt;table border=1&gt;<br> &lt;tr&gt;&lt;td&gt;<br> <br> Q1 What is the position &lt;BR&gt;&lt;font size=&quot;1&quot;&gt;<br><br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;10&quot;&gt;North East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;9&quot;&gt;East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;8&quot;&gt;North<br> &lt;BR&gt; <br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;7&quot;&gt;North West<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;6&quot;&gt;South East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;5&quot;&gt;West<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;4&quot;&gt;South<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;pos&quot; Value=&quot;3&quot;&gt;South West<br> <br> &lt;/font&gt;<br><br> &lt;/td&gt;&lt;/tr&gt;<br><br> &lt;tr&gt;&lt;td&gt;<br> <br> Q2 Where is the Location? &lt;br&gt;&lt;font size=&quot;1&quot;&gt;<br> <br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;10&quot;&gt;South East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;9&quot;&gt;North West<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;8&quot;&gt;East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;7&quot;&gt;West<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;6&quot;&gt;South <br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;5&quot;&gt;North<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;4&quot;&gt;North East<br> &lt;BR&gt;<br> &lt;INPUT TYPE=&quot;radio&quot; NAME=&quot;loc&quot; Value=&quot;3&quot;&gt;South West<br><br> &lt;/font&gt;<br> <br> &lt;/td&gt;&lt;/tr&gt;<br><br> &lt;tr&gt;&lt;td&gt;<br><br> &lt;INPUT type=button value=&quot;Get your Location&quot; onClick=&quot;calc(testform);&quot;&gt; <br>   <br> &lt;INPUT type=reset value=clear&gt; <br> &lt;/TD&gt;&lt;/TR&gt;<br><br> &lt;TR colspan=2&gt;&lt;TD align=&quot;center&quot;&gt;&lt;FONT face=arial size=2&gt;<br> <br> Result &lt;/FONT&gt;<br> <br> &lt;FONT size=3&gt;<br><br> &lt;INPUT name=result size=4&gt;&lt;/FONT&gt;<br> &lt;/TD&gt;&lt;/TR&gt;<br> &lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/FORM&gt;<br><br>&lt;table border=&quot;1&quot; summary=&quot;Total Location&quot;&gt;<br><br> &lt;tr&gt;Your Location<br> <br> &lt;th&gt;No.&lt;/th&gt; &lt;th&gt;Questions&lt;/th&gt; &lt;th&gt;Points&lt;/th&gt; &lt;th&gt; Your Location&lt;/th&gt;<br><br> &lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;<br> &lt;td&gt;Your position is&lt;/td&gt;<br> <br> &lt;td align=&quot;right&quot;&gt;10&lt;/td&gt;<br> &lt;td align=&quot;right&quot;&gt;&lt;!-- Selected Position value to display --&gt; &lt;/td&gt;<br> &lt;/tr&gt;<br><br> &lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;<br> &lt;td&gt;Your Location is &lt;/td&gt;<br> <br> &lt;td align=&quot;right&quot;&gt;10&lt;/td&gt;<br> &lt;td align=&quot;right&quot;&gt;&lt;!-- Selected Location value to display --&gt;&nbsp;&nbsp;&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;tr&gt;&lt;td&gt; &lt;/td&gt;<br> &lt;td align=&quot;right&quot;&gt;&lt;B&gt;Total &lt;/B&gt;&lt;/td&gt;<br> &lt;td align=&quot;right&quot;&gt;&lt;B&gt;100&lt;/B&gt;&lt;/td&gt;<br> &lt;td align=&quot;right&quot;&gt;&lt;B&gt; &lt;!-- Total value to display --&gt; &lt;/B&gt;&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;/tr&gt;<br>&lt;/table&gt;&lt;/div&gt;<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;
 
there are no brackets in switch statements. So, instead of this:<br><FONT FACE=monospace>switch(loc)<br>{<br>case 10:{t1=10; break; }<br>case 9: {t1=9;&nbsp;&nbsp;break; }<br>case 8: {t1=8;&nbsp;&nbsp;break; }<br>case 7: {t1=7;&nbsp;&nbsp;break; }<br>case 6: {t1=6;&nbsp;&nbsp;break; }<br>case 5: {t1=5; break; }<br>case 4: {t1=4; break; }<br>case 3: {t1=3; break; }<br><br>default:<br>alert(&quot;You Must Select&quot;); break;<br>}</font><br><br>you'd want this:<br><FONT FACE=monospace>switch(loc)<br>{<br>case 10: t1=10; break;<br>case 9:&nbsp;&nbsp;t1=9;&nbsp;&nbsp;break;<br>case 8:&nbsp;&nbsp;t1=8;&nbsp;&nbsp;break;<br>case 7:&nbsp;&nbsp;t1=7;&nbsp;&nbsp;break;<br>case 6:&nbsp;&nbsp;t1=6;&nbsp;&nbsp;break;<br>case 5:&nbsp;&nbsp;t1=5;&nbsp;&nbsp;break;<br>case 4:&nbsp;&nbsp;t1=4;&nbsp;&nbsp;break;<br>case 3:&nbsp;&nbsp;t1=3;&nbsp;&nbsp;break;<br>default: alert(&quot;You Must Select&quot;); break;<br>}</font><br><br>That should work...<br> <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Dear Liam and ALad,<br><br>&gt; there are no brackets in switch statements.<br><br>While brackets are not required inside the cases of a switch statement they are allowed and should not cause any errors in the way ALad's original code is written.<br><br>I have not check all the code but there are several problems here:<br><br>function calc(testform){<br>var ttotal1;<br>call function position(pos);<br>{ttotal1 = parseInt(t); }<br>function location(loc);<br>{ttotal1&nbsp;&nbsp;= ttotal1 + parseInt(t1);}<br>}<br><br>That code is just a mess. First:<br><br>call function position(pos);<br><br>I don't have any idea what the scripting engine might do with that expression! I would hope an error would be produced if there is no 'call' statement in Javascript which I don't think there is. What I think you want to do is this:<br><br>postion(pos);<br><br>Then you call your 'location' function<br>location(loc);<br><br>which defines a local variable named 't1' then you try to access the local variable in the outer scope:<br>ttotal1&nbsp;&nbsp;= ttotal1 + parseInt(t1);<br><br>That's just not going to work. You need to do a little more studying of 'variable scope' in programming concepts. I would think the browser would generate an error at the above line if it ever gets to it.<br><br>Good luck<br>-pete<br><br>
 
thanks, Pete... I didn't want to look through all that code, so I just put down the first thing that didn't look right. Goes back to the &quot;necessary code to reproduce the problem&quot; issue... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Liam,<br><br>&gt; Goes back to the &quot;necessary code to reproduce the problem&quot; issue<br><br>THANKS! I didn't know anyone was listening! ;o)<br><br>-pete<br><br>
 
<b>Have a look at the Script only, have Improvised it. <br>Only Trouble is it does not goes for calculation, it just displays the default message twice and ttotal=0.</b><br>&nbsp;<br><br>&lt;html&gt;<br>&lt;script language=&quot;JavaScript&quot;&gt;<br><br>var t=0, t1=0;<br><br>function location1(x){<br> switch(x)<br> {<br> case 10:t=10; return t=10; break; <br> case 9: t=9; return t=9;&nbsp;&nbsp;break; <br> case 8: t=8; return t=8; break; <br> case 7: t=7; return t=7; break; <br> case 6: t=6; return t=6; break; <br> case 5: t=5; return t=5; break; <br> case 4: t=4; return t=4; break; <br> case 3: t=3; return t=3; break; <br> default:<br> &nbsp;&nbsp;&nbsp;alert(&quot;You Must Select&quot;); break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t;<br>}<br><br>function location2(y){<br> switch(y)<br> {<br> case 10: t1=10; return t1=10; break; <br> case 9: t1=9; return t1=9; break; <br> case 8: t1=8; return t1=8; break; <br> case 7: t1=7; return t1=7; break; <br> case 6: t1=6; return t1=6; break; <br> case 5: t1=5; return t1=5; break; <br> case 4: t1=4; return t1=4; break; <br> case 3: t1=3; return t1=3; break; <br> default:<br> &nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;You Must Select&quot;); break;<br> }<br> return t1;<br>}<br><br><font color=red><br>function calc(testform){<br> var ttotal1;<br> location1(testform.loc1)<br> ttotal1 = parseInt(t);<br> location2(testform.loc2)<br> ttotal1 = ttotal1 + parseInt(t1);<br> alert(&quot; TOTAL -&gt; &quot; + ttotal1)<br>return true;<br>} <br></font><br>&lt;/script&gt;<br><br><br><br><br><br><br><br><b>&lt;!-- BELOW IS HTML for&nbsp;&nbsp;BROWSING --&gt;</b><br>&lt;form name=&quot;testform&quot; &gt; <br>&lt;table BORDER &gt;<br> &lt;tr&gt;<br> &lt;td&gt;Q1 What is the position &lt;br&gt;<br><br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;10&quot;&gt;&lt;font size=-2&gt;North East&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;9&quot;&gt;&lt;font size=-2&gt;East&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;8&quot;&gt;&lt;font size=-2&gt;North&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;7&quot;&gt;&lt;font size=-2&gt;North West&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;6&quot;&gt;&lt;font size=-2&gt;South East&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;5&quot;&gt;&lt;font size=-2&gt;West&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;4&quot;&gt;&lt;font size=-2&gt;South&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc1&quot; Value=&quot;3&quot;&gt;&lt;font size=-2&gt;South West &lt;/font&gt;<br> &lt;/td&gt;<br>&lt;/tr&gt;<br><br>&lt;tr&gt;<br>&lt;td&gt;Q2 Where is the Location?&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;10&quot;&gt;&lt;font size=-2&gt;South East &lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;9&quot;&gt;&lt;font size=-2&gt;North West&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;8&quot;&gt;&lt;font size=-2&gt;East&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;7&quot;&gt;&lt;font size=-2&gt;West&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;6&quot;&gt;&lt;font size=-2&gt;South &lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;5&quot;&gt;&lt;font size=-2&gt;North&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;4&quot;&gt;&lt;font size=-2&gt;North East&lt;/font&gt;&lt;br&gt;<br>&lt;input TYPE=&quot;radio&quot; NAME=&quot;loc2&quot; Value=&quot;3&quot;&gt;&lt;font size=-2&gt;South West &lt;/font&gt;<br>&nbsp;&lt;/td&gt;<br>&lt;/tr&gt;<br><br><br><br><br>&lt;!-- Calculation PART&nbsp;&nbsp;--&gt;<br>&lt;tr&gt;<br>&lt;td&gt;<br><b><br>&lt;input type=button value=&quot;Get your Location&quot; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onClick=&quot;calc(testform);&quot;&gt;</b><br><br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=reset value=clear&gt;<br><br>&nbsp;&lt;/td&gt;<br>&lt;/tr&gt;<br><br>&lt;tr colspan=&quot;2&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td ALIGN=CENTER&gt;&lt;font face=&quot;arial&quot;&gt;&lt;font size=-1&gt;Result&lt;/font&gt;&lt;/font&gt;&lt;input name=result size=4&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;/table&gt;<br>&lt;/form&gt;<br>&lt;/center&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;
 
arrgh :eek:) If I'm only supposed to be looking at the script, don't post what I don't need to see... sorry, I don't mean to complain- but scrolling past unnecessary data alone can be painful... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Hi, <br>Had given the HTML statement to try it out and see the message.&nbsp;&nbsp;Nothing much about it.<br><br>Awaiting,<br><br>Lad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top