The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Hello,
I have to create a calculator using Java script (html file) and MUST follow the assignment requirements (file attached). I tired and received the below feedback. Any help in understanding and modifying my code to fulfill the requirements would be much appreciated. I have cut and paste my calculator file in .txt and cut and paste it below.
Â
Feedback:
When I run your calculator, it proceeds to do the calculations and display the result of the calculation even when there are errors in the values that were entered. This is due to the fact that the error checking is done when the onblur event occurs. However, when the calculation button is clicked, the result is displayed regardless of whether there was an error.
Â
Please modify your program to call the numberCheck() function within each of the calculation functions (do not use the onblur event).
The numberCheck() function should return a value of false when a value entered in the textboxes does not meet the requirements specified in the assignment. Currently, the numberCheck() function only returns a value of false for some of the error conditions.
Â
In each of the calculation functions, add(), sub(), mul(), and div() the program should only do the calculation and display the result when the numberCheck() function returns a value of true.
Â
This is the code that I have so far:
<html>
<head>
<title>Calculator of Freedom</title>
<script language="JavaScript" type="text/javascript">
// Declare the variables
var add, subtract, divide, multiply;
// Form set up
// Make sure the text box just used is filled with a number
Â
function numberCheck(elementName) {
     switch (elementName)
     {
      case "val1":
Â
Â
      var tmp = parseInt(document.calculateForm.val1.value);
Â
if(tmp == "")
alert("Please enter number in first block");
if(tmp>9999 || tmp<-9999)
alert("please enter number between 9999 and -9999");
Â
Â
      if (isNaN(tmp))
      {
        alert("Please enter a valid number value into the first text box.");
        document.getElementById("val1").value=0;Â
        return false;
      }
      break;
 Â
      case "val2":
      var tmp = parseInt(document.calculateForm.val2.value);
if(tmp == "")
alert("Please enter number in second block");
if(tmp>9999 || tmp<-9999)
alert("please enter number between 9999 and -9999");
 Â
    if (isNaN(tmp))
      {
        alert("Please enter a valid number value into the second text box.");
        document.calculateForm.val2.value=0;
        return false;
      }
      break;
     }
 Â
     return true;
}
// This should do the requested calculation I hope
function add() {
     // Here should be the temporary place to store the result
     var result = 0;
 Â
     // Convert the values in the box t0 get numbers
     var x = parseInt(document.getElementById("val1").value);
     var y = parseInt(document.getElementById("val2").value);
 Â
     // Figures out which operation needs to be performed and does
  result = x + y;
Â
Â
 Â
     document.calculateForm.val3.value = result;
     }
function sub() {
     // Another temp place for result
     var result = 0;
 Â
     // convert the values in the boxes to get numbers
     var x = parseInt(document.calculateForm.val1.value);
     var y = parseInt(document.calculateForm.val2.value);
 Â
     // work out which operation needs to be performed and do it
  result = x - y;
 Â
     document.calculateForm.val3.value = result;
     }
Â
function mul() {
     // Another temp place for results
     var result = 0;
 Â
     // convert the values in the boxes into numbers
     var x = parseInt(document.calculateForm.val1.value);
     var y = parseInt(document.calculateForm.val2.value);
 Â
     // Figures out which operation needs to be performed and does it
  result = x * y;
 Â
     document.calculateForm.val3.value = result;
     }
Â
function div() {
     // temp place
     var result = 0;
 Â
     // convert the values in the box into numbers
     var x = parseInt(document.calculateForm.val1.value);
     var y = parseInt(document.calculateForm.val2.value);
  Â
     {
      if (y == 0)
      {
        alert ("Nice try but you can't divide by 0!");
      }
 Â
      else
      {
        result = x / y;
      }
     }
 Â
     document.calculateForm.val3.value = result;
     }
Â
</script>
</head>Â
<body>
Â
  <form action="post" name="calculateForm" id="calculateForm">
Â
  <input type="text" name="val1" value="0" id="val1" size="10" onblur="numberCheck(name);" />
Â
  <input type="text" name="val2" value="0" id="val2" size="10" onblur="numberCheck(name);" />
   =
Â
  <input name="val3" type="text" size="10" readonly="readonly" />
<br>
<br>
<input type="button" value="Add" onclick="add();" />
<input type="button" value="Sub" onclick="sub();" /><br><br>
<input type="button" value="Mul" onclick="mul();" />
<input type="button" value="Div" onclick="div();" />
</form>
</body>
</html>
Attachments:
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly