Lab 11: JavaScript Operators

In this lab, you will work with JavaScript operators.

Objectives:

Part 1: JavaScript Operators

Steps:

  1. Use File - Close All to close all open editing windows.

  2. In RAD, create a new Dynamic Web Project named Lab11Web associated with Lab11EAR.

  3. In the WebContent folder, create a new HTML File named math.html that uses an external JavaScript named factorial.js to calculate 10 factorial. Write the result to the page.

  4. Update the last part to use another external JavaScript named compound-interest.js so that the page also prints out the future value of an investment given this formula:

    P = C(1 + r/n) to the power of nt
    
    P = Future value
    C = Initial deposit
    r = Interest rate as a percentage fraction (e.g. 0.075) is 7.5
    n = number of times interest calculated per year
    t = number of years
           

    Use values of:

    C = 10000
    r = 0.075
    t = 5
    y = 4       
           

    Given those values, the result should be 14499.480257195399.

  5. Update the last part so it references another external JavaScript named divide-by-zero.js so it displays the result of dividing 100 by 0.

  6. Update the last part so it references another external JavaScript named conversions.js that implements the quiz on the last page of the Operators chapter. Your script should print the result of each question to the page.

Part 2: More Practice

Steps:

  1. Write a JavaScript program to solve the quadratic formula:

    1. Google the "quadratic formula" - it solves equations of the form a(x**2) + bx + c = 0 and results in two answers (the formula has a + or - in it). Study the equation and think about how you could implement it.

    2. Create a new HTML file named quadratic.html with an internal Javascript in the head. The script should contain a function named solvequadratic that accepts a, b and c:

      function solvequadratic(a,b,c)
      {
      }        
                  

    3. Complete the function so that it calculates both roots of the quadratic formula given the input values and displays the two roots in an alert box.

      In the body, create an HTML form with three text input widgets that let the user enter a, b and c as well as a submit button. When the user submits the form, invoke the function, passing whatever the user entered into the three input widgets.

    4. Test with values 1, 3 and -4. The roots should be 1.0 and -4.0.