Download presentation
Presentation is loading. Please wait.
Published byLynn Parks Modified over 9 years ago
1
JAVASCRIPT TIPS
2
REMEMBER JAVASCRIPT IS VERY, VERY CASE SENSITIVE
3
RESERVED WORDS List by category List by category List by category List by category Alphabetical list under resources Alphabetical list under resources
4
JAVASCRIPT CONSOLE Shows errors Shows errors Lets you write messages and intermediate results Lets you write messages and intermediate results console.log ( whatever helps);
5
USING JSFIDDLE Validation Validation Testing Testing Cut and paste Cut and paste Add HTML and CSS if you are having problems Add HTML and CSS if you are having problems
6
KEY CONCEPTS: VARIABLES AND FUNCTIONS
7
VARIABLES A place to hold a value A place to hold a value Mailbox: know where to pick up my mail; don’t know what’s in it Mailbox: know where to pick up my mail; don’t know what’s in it How to define? How to define? var name; var name = initial-value;
8
FUNCTION: COLLECTION OF INSTRUCTIONS HTML JAVASCRIPT (function.js) function doit () { alert(“Hi!”); }
9
WHAT WE WANT TO DO
10
FORM WITH INPUT, BUTTON, OUTPUT HTMLJavaScript
11
ADD DATA HTMLJavaScript
12
PUSH BUTTON AND INPUT DATA SENT TO JAVASCRIPT HTMLJavaScript
13
PARAMETERS Just a special type of variable Just a special type of variable Something that you hand to the function Something that you hand to the function Q: Many users: how do you name? Q: Many users: how do you name? A: Give it its OWN names to use locally A: Give it its OWN names to use locally Q: How do you match up? Q: How do you match up? A: By POSITION A: By POSITION
14
FUNCTION WITH PARAMETERS HTML JAVASCRIPT (function.js) function doit (a,b) { var c = a*b); alert(“product is ”+c); }
15
JAVASCRIPT USES THE DATA TO CREATE A NEW RESULT HTMLJavaScript
16
AND MOVES IT TO THE OUTPUT LOCATION HTMLJavaScript
17
RETURN VALUE return (value); Want to get information BACK to HTML Want to get information BACK to HTML With a return, the function has a VALUE With a return, the function has a VALUE Can be used anywhere you can use a constant or variable Can be used anywhere you can use a constant or variable Alert Alert Assignment statement Assignment statement Can only change one thing with a return Can only change one thing with a return
18
FUNCTION WITH RETURN HTML JAVASCRIPT (function.js) function doit (a,b) { var c = a*b; return(c); }
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
20
DOING INTERESTING THINGS WITH JAVASCRIPT
21
ASSIGNMENT STATEMENTS target = new-value; CHANGE the value of the target variable TO the new-value CHANGE the value of the target variable TO the new-value new-value can be a constant, a variable, or an expression new-value can be a constant, a variable, or an expression x = 3; x = y; x = x+ 5;
22
ARRAYS Collection of related information Collection of related information Easy way to choose between items Easy way to choose between items var array = [ “A", "B", “F", "G" ]; var array = [ “A", "B", “F", "G" ]; array[index] array[index] Example: user enters number, you return that month Example: user enters number, you return that month
23
RANDOM SELECTION Choose between options randomly 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!
24
CONVERTING RANDOM TO INTEGER Often useful to convert that random number to an 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) 0->1 needs to be changed to 0->3 (or any other number) var biggerNumber = n*4; gets the range correct var biggerNumber = n*4; gets the range correct But only want integer: But only want integer: Math.floor returns the largest integer less than the value var biggerInteger = Math.floor(n*4); var biggerInteger = Math.floor(n*4);
25
PUTTING CONTENT WITHIN TAGS General form: context.element.attribute So far we have form-name.input-id.value form-name.input-id.value form-name.img-id.src form-name.img-id.src
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.