CSC 160 - Computer Programming for Non-majors

Assignment #2: Checking for Bad Data

Due Thursday, October 7, 2010

Below you will find a program that checks for data when converting from pounds to kilograms. Once you are sure that this program is running correctly, add an additional test: if the weight is greater than 500 kilograms, print a message saying this weight is above the limit.

You will submit the listing as the assignment.

<html>
  <head>
    <title>CSC 160 Lab Exercise #1</title>
  </head>

  <body>
    <script type="text/javascript">
      // Your name goes here

      // Convert pounds to kilograms
      // Input - weight in pounds
      // Output - weight in kilograms
      // Get the weight in pounds
      var lbs = parseFloat(prompt("What is the weight in pounds?", 0.0));

      // Ensure that the weight in pounds is
      // valid. If it is valid, calculate and
      // display the weight in kilograms
      if (lbs < 0)
        alert(lbs + " is not a valid weight.");
      else {
        var kg = lbs / 2.2;
        alert("The weight is " + kg + " kilograms");
      }
    </script>
  </body>
</html>

[Back to the Lab Assignment List]