TIPS REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE RESERVED - - PowerPoint PPT Presentation

tips remember javascript
SMART_READER_LITE
LIVE PREVIEW

TIPS REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE RESERVED - - PowerPoint PPT Presentation

JAVASCRIPT TIPS REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE RESERVED WORDS List by category Alphabetical list under resources JAVASCRIPT CONSOLE Shows errors Lets you write messages and intermediate results console.log (


slide-1
SLIDE 1

JAVASCRIPT TIPS

slide-2
SLIDE 2

REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE

slide-3
SLIDE 3

RESERVED WORDS

  • List by category
  • Alphabetical list under resources
slide-4
SLIDE 4

JAVASCRIPT CONSOLE

  • Shows errors
  • Lets you write messages and intermediate results

console.log ( whatever helps);

slide-5
SLIDE 5

USING JSFIDDLE

  • Validation
  • Testing
  • Cut and paste
  • Add HTML and CSS if you are having problems
slide-6
SLIDE 6

KEY CONCEPTS: VARIABLES AND FUNCTIONS

slide-7
SLIDE 7

VARIABLES

  • A place to hold a value
  • Mailbox: know where to pick up my mail; don’t know what’s

in it

  • How to define?

var name; var name = initial-value;

slide-8
SLIDE 8

FUNCTION: COLLECTION OF INSTRUCTIONS

HTML

<head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit();”> </body>

JAVASCRIPT (function.js)

function doit () { alert(“Hi!”); }

slide-9
SLIDE 9

WHAT WE WANT TO DO

slide-10
SLIDE 10

FORM WITH INPUT, BUTTON, OUTPUT

HTML JavaScript

slide-11
SLIDE 11

ADD DATA

HTML JavaScript

slide-12
SLIDE 12

PUSH BUTTON AND INPUT DATA SENT TO JAVASCRIPT

HTML JavaScript

slide-13
SLIDE 13

PARAMETERS

  • Just a special type of variable
  • Something that you hand to the function
  • Q: Many users: how do you name?
  • A: Give it its OWN names to use locally
  • Q: How do you match up?
  • A: By POSITION
slide-14
SLIDE 14

FUNCTION WITH PARAMETERS

HTML

<head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“doit(3,5);”> </body>

JAVASCRIPT (function.js)

function doit (a,b) { var c = a*b); alert(“product is ”+c); }

slide-15
SLIDE 15

JAVASCRIPT USES THE DATA TO CREATE A NEW RESULT

HTML JavaScript

slide-16
SLIDE 16

AND MOVES IT TO THE OUTPUT LOCATION

HTML JavaScript

slide-17
SLIDE 17

RETURN VALUE

return (value);

  • Want to get information BACK to HTML
  • With a return, the function has a VALUE
  • Can be used anywhere you can use a constant or variable
  • Alert
  • Assignment statement
  • Can only change one thing with a return
slide-18
SLIDE 18

FUNCTION WITH RETURN

HTML

<head> <script src=“function.js”></script> </head> <body> <button type=“button” onclick=“alert(doit(3,5));”> </body>

JAVASCRIPT (function.js)

function doit (a,b) { var c = a*b; return(c); }

slide-19
SLIDE 19

CHANGING MORE THAN ONE THING

If you have two things that you want to change Can change them in the function Usually do NOT return value Can only use such a function in one place

slide-20
SLIDE 20

DOING INTERESTING THINGS WITH JAVASCRIPT

slide-21
SLIDE 21

ASSIGNMENT STATEMENTS

target = new-value;

  • CHANGE the value of the target variable TO the new-value
  • new-value can be a constant, a variable, or an expression

x = 3; x = y; x = x+ 5;

slide-22
SLIDE 22

ARRAYS

  • Collection of related information
  • Easy way to choose between items
  • var array = [ “A", "B", “F", "G" ];
  • array[index]
  • Example: user enters number, you return that month
slide-23
SLIDE 23

RANDOM SELECTION

Choose between options randomly var n = Math.random(); [Math is a collection of functions] If you use it twice, you get two different values. Need to save it to reuse!

slide-24
SLIDE 24

CONVERTING RANDOM TO INTEGER

  • Often useful to convert that random number to an integer

Index into array!

  • 0->1 needs to be changed to 0->3 (or any other number)
  • var biggerNumber = n*4; gets the range correct
  • But only want integer:

Math.floor returns the largest integer less than the value

  • var biggerInteger = Math.floor(n*4);
slide-25
SLIDE 25

PUTTING CONTENT WITHIN TAGS

General form: context.element.attribute So far we have form-name.input-id.value form-name.img-id.src