javascript tips - computer science - welcome to the · pdf filejavascript tips. remember...

25
JAVASCRIPT TIPS

Upload: doanque

Post on 14-Feb-2018

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

JAVASCRIPTTIPS

Page 2: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

REMEMBERJAVASCRIPT

IS VERY, VERY

CASE SENSITIVE

Page 3: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

RESERVED WORDS

• List by category

• Alphabetical list under resources

Page 4: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

JAVASCRIPT CONSOLE

• Shows errors

• Lets you write messages and intermediate results

console.log ( whatever helps);

Page 5: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

USING JSFIDDLE

• Validation

• Testing

• Cut and paste

• Add HTML and CSS if you are having problems

Page 6: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

KEY CONCEPTS: VARIABLES AND FUNCTIONS

Page 7: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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;

Page 8: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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!”);

}

Page 9: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

WHAT WE WANT TO DO

Page 10: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

FORM WITH INPUT, BUTTON, OUTPUT

HTML JavaScript

Page 11: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

ADD DATA

HTML JavaScript

Page 12: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

PUSH BUTTON AND INPUT DATA SENT TO JAVASCRIPT

HTML JavaScript

Page 13: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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

Page 14: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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);

}

Page 15: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

JAVASCRIPT USES THE DATA TO CREATE A NEW RESULT

HTML JavaScript

Page 16: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

AND MOVES IT TO THE OUTPUT LOCATION

HTML JavaScript

Page 17: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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

Page 18: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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);

}

Page 19: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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

Page 20: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

DOING INTERESTING THINGS WITH JAVASCRIPT

Page 21: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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;

Page 22: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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

Page 23: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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!

Page 24: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

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);

Page 25: JAVASCRIPT TIPS - Computer Science - Welcome to the · PDF fileJAVASCRIPT TIPS. REMEMBER JAVASCRIPT IS VERY, ... •Something that you hand to the function •Q: Many users: how do

PUTTING CONTENT WITHIN TAGS

General form: context.element.attribute

So far we have

form-name.input-id.value

form-name.img-id.src