Question

In: Computer Science

Need to modify so that it uses a function. We are taking a number and rounding...

Need to modify so that it uses a function. We are taking a number and rounding it to 2 from the decimal.

<!doctype html>
<html>
<head>
   <title> NumberRounder </title>
</head>

<body>
   <h2>Number Rounder</h2>
   <p>
   Enter a number: <input type="text" id="numberBox" size=12 value=3.14159>
   </p>
   <input type="button" value="Round It"
       onclick="number=parseFloat(document.getElementId('numberBox').value);
               rounded=Math.round(number*100)/100;
               document.getElementId('outputDiv').innerHTML=
                   number + ' rounded to one decimal place is ' + rounded;">
  
   <hr>
   <div id="outputDiv"></div>
</body>
</html>

Solutions

Expert Solution

Hi,

Please see the below code which is working and expected output is showing. Seperate function has been added and called it from the onclick event.

<html>
<head>
    <title> NumberRounder </title>
    <script>
        function myClick(e){
        var number=parseFloat(document.getElementById('numberBox').value);
               rounded=Math.round(number*100)/100;
               document.getElementById('outputDiv').innerHTML=
                   number + ' rounded to one decimal place is ' + rounded;
        }
    </script>
</head>

<body>
    <h2>Number Rounder</h2>
    <p>
        Enter a number: <input type="text" id="numberBox" size="12" value="3.14159">
    </p>
    <input type="button" value="Round It" onclick="myClick()">

    <hr>
    <div id="outputDiv"></div>

</body>
</html>

The only change which is required in your program is :

Use document.getElementById instead of document.getElementId


Related Solutions

Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number...
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number of comparisons it makes during a sort and displays the totals. To count comparisons, you will need to break up the double condition in the inner while loop. Use this program to measure the number of copies and comparisons for different amounts of inversely sorted data. Do the results verify O(N2 ) efficiency? Do the same for almost-sorted data (only a few items out...
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
Enhance the Future Value application Modify this application so it uses a persistent session to save...
Enhance the Future Value application Modify this application so it uses a persistent session to save the last values entered by the user for 2 weeks.. Index.php -------------------------- <?php     //set default value of variables for initial page load     if (!isset($investment)) { $investment = '10000'; }     if (!isset($interest_rate)) { $interest_rate = '5'; }     if (!isset($years)) { $years = '5'; } ?> <!DOCTYPE html> <html> <head>     <title>Future Value Calculator</title>     <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body>    ...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons. /* * The source code for this assignment started with * a sample from "Thinking in Java" 3rd ed. page 825 * by Bruce Eckel. I have finished adding the rest of the action...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT