views:

27

answers:

2

I'm try to create a simple form that spits out a measurement. For Example 32 B should be (32+5)=37

I'm not sure whats wrong. Please help~ Thanks so much!

<HTML>
<HEAD>
<TITLE>Bra Size to Chest Size</TITLE>

<SCRIPT LANGUAGE="JavaScript">

function CalculateSum(Atext, Btext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(this.CuptoNum(Btext));
form.Answer.value = A + B;
}

/* ClearForm: this function has 1 argument: form.
   It clears the input and answer fields on the form.
   It needs to know the names of the INPUT elements in order
   to do this. */

function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.Answer.value = "";
}

function CuptoNum(str)
{
if((str >= 0) && (str <= 9))
return str;
switch(str.toUpperCase()) {
case "A": return 4;
case "B": return 5;
case "C": return 6;
case "D": return 7;
case "E": return 8;
case "F": return 9;
default:  alert('You must choose a number between 0 and 9 or a letter between A and F!');
return 'X';
}


// end of JavaScript functions -->
</SCRIPT>
</HEAD>

<BODY>

<P><FONT SIZE="+2">Simple Adder</FONT></P>

<FORM NAME="Calculator" METHOD="post">
<P>Enter a number: <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P>
<P>Enter a number: <INPUT TYPE=TEXT NAME="input_B" SIZE=10></P>
<P><INPUT TYPE="button" VALUE="Add Numbers" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form)"></P>
<P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P>
<P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P>
</FORM>

</BODY>
</HTML>
A: 

Your CuptoNum function starts off with a check if str is between 0 and 9, while clearly, it will not be a number at all. You want to change that to something like

if("ABCDEF".indexOf(str.toUpperCase() == -1))
    return str;

or remove that check altogether, as you have a default clause in your switch.

David Hedlund
did not notice that `return` keyword, thanks
Sarfraz
+1  A: 

change this.CuptoNum(Btext) to CuptoNum(Btext);

there is no need to use 'this' object. you are not refering to any class here.

coder