Docsity

Javascript math functions, Slides of Javascript programming

Toggle sidebar
bg1
Please send any comments and/or corrections to LFS
Javascript functions for GeoGebra - ggb applet part is in NAVY
In and Out Functions
setScalar // assigns the ggb variable "objName" the value objValue
Existing // tests that the ggb object "objName" exists
CheckAnswer // checks numeric value "objName1"=numeric value "objName2" within 2decimals
GridPoint // tests that the point "objName" is a grid point (has whole number coords)
SamePoint // tests that the points "objName1" & "objName2" are not the same point
GoodPoints // tests that points "objName1" & "objName2" are not on same vertical or horizontal
PointLine // tests that the point "objName" is on the line with slope=za and y-intercept=zb
FindSlopeInt // finds slope and y-intercept of the line through points "objName1" & "objName2"
Some Math Functions
gensetNN2 // function generates 2 unequal random Natural Numbers and assigns them to variables "objName1" & "objName2" in ggb
gensetAngle // generates random integer between 15 and 75, changes it to radians and assigns it to variable "objName" in ggb
gensetSgn // generates random +1 or -1 and assigns to ggb variable "objName" in ggb
my Functions
myRound // function rounds variable x to n decimal places
myFloor // function truncates the number after adding a tiny epsilon to avoid computer rounding errors.
In and Out Functions Comments
1. <script type="text/javascript">
function setScalar(objName, objValue) {
// assigns the ggb variable objName the value objValue
// objValue must be a number, objName will be a numeric variable
// substitutes "applet" for "document.ggbApplet" to make function shorter
var applet = document.ggbApplet;
// e.g. for input objName='num' and objValue=3 function builds string
// 'num = 3' and lets applet "evaluate this input" in ggb
applet.evalCommand(objName + " = " + x );
pf3
pf4
pf5

Partial preview of the text

Download Javascript math functions and more Slides Javascript programming in PDF only on Docsity!

Please send any comments and/or corrections to LFS

Javascript functions for GeoGebra - ggb applet part is in NAVY

In and Out Functions setScalar // assigns the ggb variable "objName" the value objValue Existing // tests that the ggb object "objName" exists CheckAnswer // checks numeric value "objName1"=numeric value "objName2" within 2 decimals GridPoint // tests that the point "objName" is a grid point (has whole number coords) SamePoint // tests that the points "objName1" & "objName2" are not the same point GoodPoints // tests that points "objName1" & "objName2" are not on same vertical or horizontal PointLine // tests that the point "objName" is on the line with slope=za and y-intercept=zb FindSlopeInt // finds slope and y-intercept of the line through points "objName1" & "objName2"

Some Math Functions gensetNN2 // function generates 2 unequal random Natural Numbers and assigns them to variables "objName1" & "objName2" in ggb gensetAngle // generates random integer between 15 and 75, changes it to radians and assigns it to variable "objName" in ggb gensetSgn // generates random +1 or -1 and assigns to ggb variable "objName" in ggb

my Functions myRound // function rounds variable x to n decimal places myFloor // function truncates the number after adding a tiny epsilon to avoid computer rounding errors.

In and Out Functions Comments

uses: myRound

// tests that the points "objName1" and "objName2" are not on the same vertical // or horizontal line and assigns ggb object okGP a boolean value of 1 if not and // 0 if so. // substitutes "applet" for "document.ggbApplet" to make function shorter var applet = document.ggbApplet; applet.evalCommand("okGP=1"); // finds delta x and delta y var zx = applet.getXcoord(objName1)- applet.getXcoord(objName2); var zy = applet.getYcoord(objName1)- applet.getYcoord(objName2); // if same vertical or horizontal, deletes the objName2 in ggb and alerts the user if ((zx == 0) || (zy == 0)) { applet.deleteObject(objName2); applet.evalCommand("okGP=0"); alert('You must not have the same x or y coordinates for A and B. \nClick "Reset" to start over.');} }

may need to round zx and zy to avoid computer rounding errors.

SamePoint has the stricter "and"=&& condition.

GoodPoints has the weaker "or"=|| condition.

i.e. fail SamePoint implies fail GoodPoint, but not vice-versa

applet.evalCommand("okPL"+objName+"=0"); alert('Point '+objName+' is not on the line.\nPlease reselect it.');} }

var nn0 = Math.floor(Math.random()2) // sgn=2{0,1}-1 yields +1 or - var sgn = 2*nn0 -1; // ggb assignment applet.evalCommand(objName + " = " + sgn ); }