4 Replies - 2197 Views - Last Post: 03 April 2012 - 08:31 AM

#1 StephanieKay90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-March 12

JavaScript Calculator: Why doesn't the multiplication checkbox wor

Posted 03 April 2012 - 07:59 AM

I FINALLY figured out the calculator code for an assignment. The only thing that doesn't work now is the multiplication checkbox, all the others work fine as do the errors! Does anybody see/know why the multiplication function/checkbox isn't giving me an answer?

Here's my code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	
	<head>
	
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
		<title>Assignment 10: Stephanie Hussar</title>
     
   <script language="javascript" type="text/javascript">
   
   function validate() {
   		a=Number(document.calculator.number1.value);  
   		b=Number(document.calculator.number2.value); 
	      if (a == " " || b == " ") {
	      alert("Error! Please enter a number in both operand text fields.");
	      return 0;
	      } else if (isNaN(a) || isNaN(B)/>) {
	      alert("Error! Please enter numerics only in the operand text fields!");
	      return 0;
	      }
	      else return;
	      }  
   
   function multiply(){  
   a=Number(document.calculator.number1.value);  
   b=Number(document.calculator.number2.value);  
   c=a*b;  
   document.calculator.total.value=c;  
  }  

   function addition(){  
   a=Number(document.calculator.number1.value);  
   b=Number(document.calculator.number2.value);  
  c=a+b;  
   document.calculator.total.value=c;  
   }  

   function subtraction(){  
   a=Number(document.calculator.number1.value);  
   b=Number(document.calculator.number2.value);  
   c=a-b;  
   document.calculator.total.value=c;  
   }  

   function division(){  
   a=Number(document.calculator.number1.value);  
   b=Number(document.calculator.number2.value);
   c=a/b;  
    
   if (0 == B)/> {
	    alert("Error! Division by Zero is not allowed.");
	    return 0;
        }
        else return document.calculator.total.value=c;  
  }  

    </script> 
   </head>  
    
  <body>  
  	
  	<script type="text/javascript">	
		
				var today = new Date();
				document.write("<h1><center> Stephanie's Javascript Calculator </h1></center>");
				document.write("<h5><center>" + today.toLocaleString() +"</h5></center>");

			
		</script>	
     
   
  <form name="calculator">  
  	
  			<center>
			<table width="750" border="2" cellpadding="20">
			<tr>
			<td>Operand A</td>
			<td><input type="text" name="number1"></td>
			<td>Operand B</td>
			<td><input type="text" name="number2"></td>
			</tr>
			<tr>
			<td>Add<input type="checkbox" value="ADD" id="add" name="add" onclick="javascript:addition();validate();" /></td>
			<td>Subtract<input type="checkbox" value="SUB" id="subtract" name="subtract" onclick="javascript:subtraction();validate();" /></td>
			<td>Multiply<input type="checkbox" value="MUL" id="multiply" name="multiply" onclick="javascript:multiply();validate();" /></td>
			<td>Divide<input type="checkbox" value="DIV" id="divide" name="divide" onclick="javascript:division();validate();" /></td>
			</tr>
			<tr>
			<td>Total:</td>
			<td><input type="text" name="total"></td>
			<td></td>
			<td></td>
			</tr>
			</table>
			</center>
   

  			<center><input type="reset" id="Clear" name="Clear"></center>  
  
     
  </form>  
    
  </body>  
  </html>  


Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript Calculator: Why doesn't the multiplication checkbox wor

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: JavaScript Calculator: Why doesn't the multiplication checkbox wor

Posted 03 April 2012 - 08:05 AM

what exactly is not working (i.e. what does it not do it should and vice versa)?

anything in the Error Console?

PS. you don’t need the javascript: part in the onclick attributes, Javascript is the only thing in there.

PPS. it should be radio buttons. the calculation is started once you click on that element, whether you check or uncheck it with that action doesn’t matter but will confuse the user (click "add" addition (check), click "subtract" (same numbers) subtraction (check), click "add" (same numbers) addition (uncheck) => you have made an addition but the checkmark is on subtraction)

This post has been edited by Dormilich: 03 April 2012 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 StephanieKay90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-March 12

Re: JavaScript Calculator: Why doesn't the multiplication checkbox wor

Posted 03 April 2012 - 08:14 AM

when you enter two numbers and then click the check box for multiply, it does not output an answer. The error console says the "multiply" is not a function, but when I look over the multiply function it looks correct to me. Try running the code and you will see that the multiplication isn't working correct.

the teachers specifically wants checkboxes (why she makes it so complicated, i don't know) but we HAVE to have check boxes as well as document.write()
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: JavaScript Calculator: Why doesn't the multiplication checkbox wor

Posted 03 April 2012 - 08:25 AM

IE?

in IE IDs and names are automatically converted to JS variables with that name. i.e. you overwrite the existing variable that represented your function. easiest fix, rename the function.
Was This Post Helpful? 0
  • +
  • -

#5 StephanieKay90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-March 12

Re: JavaScript Calculator: Why doesn't the multiplication checkbox wor

Posted 03 April 2012 - 08:30 AM

thank you for all your help! works like a charm now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1