Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming games Reprise on credit cards. Operators. Homework: Catch-up. Work on your JavaScript project.

Similar presentations


Presentation on theme: "Programming games Reprise on credit cards. Operators. Homework: Catch-up. Work on your JavaScript project."— Presentation transcript:

1 Programming games Reprise on credit cards. Operators. Homework: Catch-up. Work on your JavaScript project.

2 Credit cards (approximation) example of compounding –interest on the interest I could write this in one line, but easier to see in several lines nb1 = old_balance + new_purchases; nb2 = nb1-payment; if (payment<minimum) {nb2=nb2+penalty} nb3 = nb2 + nb2*monthly_interest; old_balance = nb3;

3 Other applications Shaking window Quiz show Random image on loading

4 Operators + –does addition if the operands are numbers –concatenation if the operands are strings. So, if you see 11 when you expected to see 2, you will need to change a number into a string * does multiplication / does division - does subtraction

5 Chocolate shop Example (see on-line, called calculation from forms) Customer enters number of boxes of chocolate and cocoa –uses forms Program calculates total cost and cost with tax. Prices are 'in' the code. (Later example will do proper formatting.)

6 Computation test function total(f) { var chocolateprice = 2.55; var cocoaprice = 3.15; var chocolatecost = chocolateprice * f.choc.value; var cocoacost = cocoaprice*f.cocoa.value; var taxrate =.06; var totalcost = chocolatecost + cocoacost; var totalpay = totalcost + taxrate*totalcost; var totals = "Total is " + totalcost + " with tax is " + totalpay; f.cost.value=totalcost; f.wtax.value=totalpay; return false; }

7 Application Form for Chocolate Enter number of goods in each category. Boxes of Chocolate: Boxes of Cocoa: Total:

8 Modulo operator % Gives remainder after division Clock arithmetic 4 % 12 is 4 13 % 12 is 1 12 % 12 is 0

9 Use of modulo Slideshow: replace if statement if (ns<= ++sn) { sn = 0; } with sn = (++sn) % ns;

10 Other operators comparison: these produce values true or false – >= != == logical: produce true or false – && means and (both operands are true) – || means or (one or the other or both are true) – –! Means logical negative others

11 Equality test Operator is == The = sign by itself does an assignment. if (a==b) { } NOTE: this would not be done if (a==a) but only testing two different variables OR expressions using variables

12 Looping To do more or less the same thing many times, use a for loop. This makes use of a so-called looping variable, which can be anything. The looping variable is frequently accessed in the body of the loop. for(initialize;condition; increment) { } Often need to have some code outside/prior to the loop to initialize the process

13 Example: add up elements in array Assume sales is an array. total = 0; for (i=0; i<sales.length; i++) { total += sales[i]; } Or total = 0; for (i=0; i<sales.length; i++) { total = total + sales[i]; }

14 Example: find max of elements in array Assume sales is an array. best = sales[0]; //first guess for (i=0; i<sales.length; i++) { if (sales[i]>best) { best = sales[i]; } }

15 Example: want to determine the index of the best best = sales[0]; bi = 0; for (i=0; i<sales.length; i++) { if (sales[i]>best) { bi = i; best = sales[i]; } }

16 Conditional operator (condition) ? value_if_true : value_if_false canrentcar = (age>25) ? true : false; Say taxrate is.05 unless city is NYC, in which case it is.08 taxrate = (city=="NYC") ?.08 :.05 Can nest conditional operators. Has effect of if this else if this else if this

17 Conditional operator alternatives if statement. –means you can do more than one thing –Perhaps less error prone switch statement –means you can do more than one thing –combine different possibilities –does require you to think about the break;

18 Video For each video (in terms of content), you code one element. To make sure it works in different browsers, this single video element has 3 elements. –http://faculty.purchase.edu/jeanine.meyer/html 5/video.htmlhttp://faculty.purchase.edu/jeanine.meyer/html 5/video.html For video shown only when 'the code' decides to show it, –http://faculty.purchase.edu/jeanine.meyer/html 5/quizmultiple.htmlhttp://faculty.purchase.edu/jeanine.meyer/html 5/quizmultiple.html

19 Videos You can use what is demonstrated in the previous examples to choose from among multiple videos. Each needs its own element with elements indicated the set of videos with similar content, each having a unique id. Set up a variable, say v1, v2, using document.getElementById. Start out with both visibility: hidden; in CSS. In the code at the appropriate place, change visibility and use v1.play() or v2.play(), ….

20 Global and Local variables Writing a var statement outside of any function makes the variable a global variable. It stays around (aka persists). Writing a var statement inside a function OR simply using a variable inside a function that does not have a var statement outside the function, makes the variable local. –What happens in Vegas stays in Vegas.

21 Detecting arrow keys … sometimes called 'capturing' arrow events Problem: browsers return this information in different ways

22 Code for arrow keys var keyCode = document.layers ? evt.which : document.all ? evt.keyCode : document.getElementById ? evt.keyCode : 0; This code does not use document.layers or document.all or document.getElementById directly but instead uses their existence to get the correct property. This could be used to get any keyCode. checking browsers

23 Using arrow keys to move object Strategy: combine code for moving object (in bouncing ball) code for capturing/detecting arrow pressed –detecting a keydown event –figuring out which key

24 Programming is …. putting techniques together To find out techniques, consult me, book, 'google' to get to OTHER sources –ask me to help with google Work in stages. For example, –my google search produced something buggy –first used alert –then combined with bouncing ball code

25 String operations Say title is a string (of characters). Use string attributes and methods title = "Programming Games"; title.length gives the number of characters stringname.substr(startingindex, length) –title.substr(1,3) is "rog" –title.substr(12,5) is "Games"

26 Matching Say you want values such as file names to match if they are partially alike –aviva1.jpg, aviva2.jpg, daniel1.jpg, daniel2.jpg, anne1.jpg, anne2.jpg, etc. –Determine how many letters you need (okay to use extra—2 would work) if (choice1.substr(0,3) == choice2.substr(0,3))

27 JavaScript & ActionScript ActionScript is the scripting language for Flash. It and JavaScript are based on the same standard in terms of syntax: format, punctuation, grammar. The differences will be –where the code goes –what objects are referenced in the code –ActionScript is compiled: translated all at once. NOTE: the word objects is used in both its normal and its technical meaning.

28 Finding errors Syntactic errors are analogous to errors in grammar and notation. The code is not legal JavaScript. Examples are leaving out parentheses or brackets or putting operators together. Semantic errors are the equivalent of saying something in correct English that isn’t what you wanted to say. Examples are adding instead of multiplying or using > in place of < or "I'm going to Programming Games class in Social Sciencs."

29 Error console In Firefox (mac) under Tools, click on Error console to see syntactic errors. –Click on Clear to remove errors In Firefox and some other browsers on the PC, you can type in javascript: in the location field Google Chrome: claims facilities for testing, but…I can't quite make them work.

30 Homework [Catch up, including uploading projects, preparing index.html file] Work on your project. Project due 3/21. Midterm 3/17


Download ppt "Programming games Reprise on credit cards. Operators. Homework: Catch-up. Work on your JavaScript project."

Similar presentations


Ads by Google