SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 2 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 29 Nov 2017 My Price 10.00

create a calculator using Java script (html file) and MUST

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:

Answers

(5)
Status NEW Posted 29 Nov 2017 02:11 PM My Price 10.00

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

Not Rated(0)