80 likes | 154 Views
JavaScript- conditions, Math objects. Generic Representation. If (a > b) { // some instructions here } If (a >= b) { // some instructions here } else { // some instructions here }. If (a < b) { // some instructions here } else if (c == d) { // some instructions here } else {
E N D
If (a > b) { // some instructions here } If (a >= b) { // some instructions here } else { // some instructions here } If (a < b) { // some instructions here } else if (c == d) { // some instructions here } else { // additional instructions here } Condition
If (a <= b) { // some instructions here if (d == f) { some nested instructions } } If (a != b) { // some instructions here } else { // some instructions here if (d == f) { some nested instructions } } Nested Conditions
Exercise: jex6.htmlChecking for null • In this exercise below, when you reset the form and you click Total when both input box are empty, you displayed an “NAN” • You need to check for null in the input box before you perform the addition or multiplication function. if (document.simpleForm.box1.value== "") { alert (“You need to have a value in the first box”); } else if (document.simpleForm.box1.value== "") { alert (“You need to have a value in the second box”); } else { // use the code from before to display the total }
Make a button and a message box. Need to have a form to “hold” this GUI objects You write form instructions in the body. You write the javascript function in the head. <FORM name=simpleForm> <input type="button" name="button1" value="RED" onclick="displayText()"> <BR> <TEXTAREA name="response" rows=3 cols=27> </textarea> <INPUT type="reset" value="Reset Form"> Recall: Button and Response function displayText() { document.simpleForm.response.value="You click the red button!"; } Note the difference between ALERT and COFIRM
Math objects • Calculation sometimes require you to use mathematical functions such as a3 requires you to use the power function such as Math.pow(a, 3). Examples are: Math.PI ~ Math.round(3.14) that rounds off a number Math.random() that gives you a floating pt. number between 0 and 1
Exercise: jex7.htmlLoan Calculator • Get three input values, loan amount, yearly interest rate and Number of Years. • Compute the monthly and total payments using formula below • L: Loan Amount • R: Monthly Interest Rate (YearlyRate / 12) • N: Number of payment periods (NoYrs X 12) • Output the results