<< Chapter < Page | Chapter >> Page > |
Output for script in Listing 1
When you open the file in your browser, the text shown in Figure 1 should be displayed in the browser window.
Figure 1 . Output for script in Listing 1. |
---|
degrees = 90 : radians = 1.5707963267948965
radians = 1 : degrees = 57.29577951308232radians = 3.141592653589793 : degrees = 180 |
The toRadians and toDegrees functions
Because it will frequently be necessary for us to convert between degrees and radians, I decided to write two functions that we will use to make those conversions. Thatwill eliminate the need for us to stop and think about the conversion (and possibly get it backwards) when writingcode. We will simply call the function that performs the conversion in the required direction.
The toRadians function expects to receive an input parameter describing an angle in degreesand returns the value for that same angle in radians.
The toDegrees function expects to receive an input parameter describing an angle in radiansand returns the value for that same angle in degrees.
Global variables named degrees and radians
The code in Listing 1 begins by declaring global variables named degrees and radians . The variable named degrees is initialized to 90 degrees.
The toRadians function is called to convert that value of degrees to radians. The returned value in radians is stored in the variable named radians .
Display contents of both variables
Then the document.write method is called to display the values contained in both variables, producing the first line of output text shown in Figure 1 .
Modify variable contents, convert, and display again
Following that, a value of 1 is assigned to the variable named radians . The toDegrees function is called to convert that value to degrees, and the result is stored in the variable named degrees .
Once again, the document.write method is called to display the current contents of both variables, producing the second line of output textshown in Figure 1 .
One more time
Finally, the mathematical constant, PI, is stored in the variable named radians . Then that value is converted to degrees and stored in thevariable named degrees . The current values in both variables are displayed, producing the last line of output text shown in Figure1.
And the results were...
As you can see from Figure 1 ,
A template
You might want to save your html file as a template for use with future exercises that require conversions between radians and degrees. This will beparticularly useful when we write scripts that use JavaScript's built-in trigonometric methods. Those methods deal with angles almost exclusively inradians while we tend to think of angles in degrees. We will use these two functions to perform conversions between degrees and radians whenrequired.
An exercise involving a right triangle
For the next exercise, I would like for you to create a right triangle on your graph board by placing pushpins at the following coordinates:
Notification Switch
Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?