1 / 14

JavaScript Arithmetic Operators and Decision Making

Learn about arithmetic operators in JavaScript and how to use them to perform calculations with variables. Also, discover how to make decisions using if-else statements.

monroym
Download Presentation

JavaScript Arithmetic Operators and Decision Making

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript Part 2

  2. Arithmetic Operators When working with variables it is always useful to do some calculations. Operators are the symbols used to work with variables.

  3. Arithmetic Operators

  4. Assignments • When you put a value into a variable, you are assigning that value to the variable, and you use an assignment operator to do the job. X=Y Sets X to the value of Y X+=Y Same as X = X + Y X-=Y Same as X = X – Y X*=Y Same as X = X * Y X/=Y Same as X = X / Y X%=Y Same as X = X % Y

  5. Arithmetic Operators Result = Result + 1; Shorthand: Result +=1; Result ++; ++ Result;

  6. Comparison Operators

  7. Logical Operators • ! (not) • && (and) • || (or)

  8. Decision Making if (condition) { … commands to execute if condition is true } else { … commands to execute if condition is false }

  9. Decision making: example if(Day==“Friday”){ document.write(“Have a nice weekend!”); } else{ document.write(“Good Morning!”); }

  10. Decision making: example2 If(Day==“Friday”|| Day==“Saturday”) { document.write(“Have a nice weekend!”); } else { document.write(“Good Morning!”); }

  11. Nested if then .. else statements var country = “france”; if (country==“spain”) { document.write(“buenos dias”); } else {if (country==“italy”) {document.write(“buon giorno”);} else {if (country==“france”) {document.write(“bonjour”);} } }

  12. Hands-on practice 2 • The user will be prompted for a quantity and must enter a quantity greater than 0. • If the user enters a value that is 0 or a negative number, an error message should be displayed. • If the user has inserted a positive value, write “thank you” to the screen. • Use prompt method & write the message to the document.

  13. The code:

  14. JS resources • http://www.w3schools.com/js/ • http://www.Lynda.com • http://www.youtube.com/watch?v=_cLvpJY2deo

More Related