vendredi 18 décembre 2015

Calculating Circumference and Area of Circle With Javascript - Function Not Working

I'm trying to create a simple javascript application that will ask the user to enter the radius of a circle, and in return will display the circumference and area in sentence form after the user hits "Calculate". Right now, when the user enters a number and hits "calculate", nothing happens.

The JavaScript is included in the HTML document like this:

<!DOCTYPE html>
<html>
<head>
  <meta charset = "utf-8">
  <title>Create a Circle</title>
  <h1 style="text-align:center">Create a Circle!</h1>
  <br>
  <div style="text-align:center">
  Enter the radius for your circle:
  <input type="text" id="txtRadius" size="10" />
  <br>
  <input type="button" value="Calculate" onclick="CalculateArea()"/>

  <script>

  function print() {
    var p = 
    document.createElement("p"),
    text = Array.prototype.join.call(arguments,",");
    p.textContent = text;
    document.getElementById("console").appendChild(p);
    return text;    
  }

  function CalculateCircumference() {
    var radius =
    parseInt(document.getElementById('txtRadius').value);//String to Integer

    if (0 < radius)
        print("The circumference of the circle is " + (radius * 2 * Math.PI);
    else
        print("Error - radius must be a whole number greater than 0.");
    return false;
  }

  function CalculateArea() {
    var radius = 
    parseInt(document.getElementById('txtRadius').value); //String to Integer

    if (0 < radius)
        print("The area of the circle is " + (radius * radius * Math.PI);
    else
        print("Error - radius must be a whole number greater than 0.");
    return false;
  }
</script>
</head>
<body>
</body>   
</html>

I'm new to the print method so I tried changing all prints to "alert", but this didn't do anything. Thank you for your help. Link to JSFiddle: http://ift.tt/1ULkhEy

Aucun commentaire:

Enregistrer un commentaire