Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CSIS 138 JavaScript Class2

Instructor Teresa Pelkie at Palomar College - Variables, Calculations, "if" statement

  • Login to see the comments

CSIS 138 JavaScript Class2

  1. 1. JavaScript – Class 2 <ul><ul><li>prompt() </li></ul></ul><ul><ul><li>operators </li></ul></ul><ul><ul><li>Calculations </li></ul></ul><ul><ul><li>if statement </li></ul></ul>
  2. 2. <ul><li>prompt(&quot;Please enter your name&quot;, &quot;&quot;); </li></ul>
  3. 3. <ul><li><head> </li></ul><ul><li><script language=&quot;JavaScript&quot;> </li></ul><ul><li>var myPrompt ; </li></ul><ul><li>myPrompt = prompt(&quot;Please enter your name&quot;, &quot;&quot;); </li></ul><ul><li></script> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><script language=&quot;JavaScript&quot;> </li></ul><ul><li>document.write( myPromp t); </li></ul><ul><li></script> </li></ul><ul><li></body> </li></ul>
  4. 4. <ul><li>Mathematical operators : + - / * % • </li></ul><ul><li>Precedence - * and / are performed first </li></ul><ul><li>unless you use parenthesis </li></ul><ul><li>Assignment operator: = also assigns a value </li></ul><ul><li>to a variable </li></ul><ul><li>Increment and decrement operators: ++ -- </li></ul>
  5. 5. <ul><li>Comparison operators: < <= > >= == != </li></ul><ul><li>Logical operators: && || ! </li></ul><ul><li>Commonly used with an “if” statement </li></ul>
  6. 6. <ul><li><script language=&quot;JavaScript&quot;> </li></ul><ul><li>alert(1 + 1); </li></ul><ul><li></script> </li></ul><ul><li><script language=&quot;JavaScript&quot;> </li></ul><ul><li>var firstNumber = 40; </li></ul><ul><li>var secondNumber = 20; </li></ul><ul><li>alert(firstNumber + secondNumber); </li></ul><ul><li></script> </li></ul>
  7. 7. <ul><li>NaN – Not a Number </li></ul><ul><li>a special value in JavaScript that is returned if </li></ul><ul><li>you do a calculation with a non-number character </li></ul><ul><li>IsNaN() – function </li></ul><ul><li>tests if a variable is a number or not </li></ul><ul><li>– returns a Boolean data type </li></ul><ul><li>returns true if it is a string (because a string is not a number) </li></ul><ul><li>returns false if it is a number </li></ul>
  8. 8. <ul><li><script language=&quot;JavaScript&quot;> </li></ul><ul><li>alert(isNaN(&quot;teresa&quot;)); </li></ul><ul><li></script> </li></ul>See page 6 in handout
  9. 9. <ul><li>if statement </li></ul><ul><li><script language=“JavaScript”> </li></ul><ul><li>var variable1 = 123; </li></ul><ul><li>var variable2 = 123; </li></ul><ul><li>if ( variable1 == variable2 ) </li></ul><ul><li>{ </li></ul><ul><li>alert (&quot;condition true&quot;); </li></ul><ul><li>} </li></ul><ul><li></script> </li></ul>no semicolon == means: is the same or is equal or is true the condition to be evaluated is inside the parenthesis code to be executed if the condition is true see page 7 handout
  10. 10. <ul><li>Using comparison and logical operators </li></ul><ul><li><script language=“JavaScript”> </li></ul><ul><li>var variable1 = 2; </li></ul><ul><li>var variable2 = 4; </li></ul><ul><li>var variable3 = 6; </li></ul><ul><li> </li></ul><ul><li>if ( ( variable1 < variable2 ) || ( variable2 < variable3 ) ) </li></ul><ul><li>{ </li></ul><ul><li>alert (&quot;condition true&quot;); </li></ul><ul><li>} </li></ul><ul><li></script> </li></ul>See page 8 and 9 in handout
  11. 11. <ul><li>if else statement </li></ul>see page 10 handout Gives you the ability to execute code when the condition evaluates to true and also when it evaluates to false if (condition) { code here will execute if condition is true’ } else { code here will execute if condition is false; }
  12. 12. <ul><li>if else statement </li></ul>see page 10 handout <script language=“JavaScript”> var variable1 = 123; var variable2 = 124; if (variable1 == variable2) { alert (&quot;true&quot;); } else { alert(&quot;false&quot;); } </script>
  13. 13. <ul><li>if else if statement </li></ul>see page 11 handout var variable1 = 123; var variable2 = 124; if (variable1 == variable2) { alert(&quot;equal&quot;); } else if (variable1 < variable2) { alert(&quot;less than&quot;); } else { alert(&quot;none of the above&quot;); } “ else if “block can be repeated as needed
  14. 14. <ul><li>JavaScript – Class 2 </li></ul><ul><li>Assignment 2 </li></ul>

×