acm 365

53
WEEK -1 ACM 365 ACM 262 Course Notes

Upload: snowy

Post on 06-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

ACM 365. Week -1. Javascript. Javascript. Javascript. Javascript. Javascript. Javascript. Javascript. My Web Page - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ACM  365

ACM 262 Course Notes

WEEK -1

ACM 365

Page 2: ACM  365

ACM 262 Course Notes

Javascript

Page 3: ACM  365

ACM 262 Course Notes

Javascript

Page 4: ACM  365

ACM 262 Course Notes

Javascript

Page 5: ACM  365

ACM 262 Course Notes

Javascript

Page 6: ACM  365

ACM 262 Course Notes

Javascript

Page 7: ACM  365

ACM 262 Course Notes

Javascript

Page 8: ACM  365

ACM 262 Course Notes

Javascript

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/

html4/strict.dtd"><html><head><title>My Web Page</title><script type="text/javascript">alert('hello world!');</script></head>

Page 9: ACM  365

ACM 262 Course Notes

Javascript

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/

html4/strict.dtd"><html><head><title>My Web Page</title><script type="text/javascript"

src="navigation.js"></script></head>

Page 10: ACM  365

ACM 262 Course Notes

Javascript

Page 11: ACM  365

ACM 262 Course Notes

Javascript

Page 12: ACM  365

ACM 262 Course Notes

Javascript

Page 13: ACM  365

ACM 262 Course Notes

Javascript

Page 14: ACM  365

ACM 262 Course Notes

Javascript

Page 15: ACM  365

ACM 262 Course Notes

Javascript

Page 16: ACM  365

ACM 262 Course Notes

Javascript

Page 17: ACM  365

ACM 262 Course Notes

Javascript

Page 18: ACM  365

ACM 262 Course Notes

Javascript

Page 19: ACM  365

ACM 262 Course Notes

Javascript

Page 20: ACM  365

ACM 262 Course Notes

Javascript

<script type="text/javascript">var firstName = 'Cookie';var lastName = 'Monster';document.write('<p>');document.write(firstName + ' ' + lastName);document.write('</p>');</script>

Page 21: ACM  365

ACM 262 Course Notes

Javascript

<script type="text/javascript">var name = prompt('What is your name?',

'');document.write('<p>Welcome ' + name +

'</p>');</script>

Page 22: ACM  365

ACM 262 Course Notes

Javascript

Page 23: ACM  365

ACM 262 Course Notes

Javascript

Creating an Array

To create and store items in an array, you first declare the array’s name (just as you would a variable) and then supply a list of comma separated values: each value represents one item in the list.

To indicate an array, you put the list of items between opening and closing brackets—[ ].

var days = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'];

The brackets—[ ]—are very important. You can create an empty array without any elements like this:

var playList = [];

Page 24: ACM  365

ACM 262 Course Notes

Javascript

<script type="text/javascript">var days = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri',

'Sat', 'Sun'];alert(days[0]);var i = 4;//alert(days[i]);</script>

Page 25: ACM  365

ACM 262 Course Notes

Javascript

Page 26: ACM  365

ACM 262 Course Notes

Javascript

<script type="text/javascript">var properties = ['red', '14px', 'Arial'];properties[3] = 'bold';properties[properties.length] = 'italic';properties.push('bold');properties.push('bold', 'italic', 'underlined');properties.unshift('12px');for(i=0;i<properties.length;i++){document.write(properties[i]);document.write('</br>');}</script>

Page 27: ACM  365

ACM 262 Course Notes

Javascript

Page 28: ACM  365

ACM 262 Course Notes

Javascript

Page 29: ACM  365

ACM 262 Course Notes

Javascript

Page 30: ACM  365

ACM 262 Course Notes

Javascript

Page 31: ACM  365

ACM 262 Course Notes

Javascript

Page 32: ACM  365

ACM 262 Course Notes

Adding Logic

if ( condition ) {// some action happens here}

if (score > 100) {alert('You won!');}

if (answer == 31) {alert('Correct. Saturn has 31 moons.');}

Page 33: ACM  365

ACM 262 Course Notes

Adding Logic

Page 34: ACM  365

ACM 262 Course Notes

Adding Logic

Page 35: ACM  365

ACM 262 Course Notes

Adding Logic

if (condition) {// write what happens } else if (condition2) {// write what happens } else {// write what happens }

Page 36: ACM  365

ACM 262 Course Notes

Adding Logic

<script type="text/javascript">

var num= prompt(‘Enter a number', ‘60');if (num >= 50) {alert(‘Thanks.');}

</script>

Page 37: ACM  365

ACM 262 Course Notes

Adding Logic

var fridayCash = prompt('How much money can you spend?', '');

if (fridayCash >= 50) {alert('You should go out to a dinner and a movie.');} else if (fridayCash >= 35) {alert('You should go out to a fine meal.');} else if (fridayCash >= 12) {alert('You should go see a movie.');} else {alert('Looks like you'll be watching TV.');}

Page 38: ACM  365

ACM 262 Course Notes

Adding Logic

Logical AND

if (a < 10 && a > 1) {alert("The value " + a + " is between 1 and 10");}

Logical OR

if (key == 'n' || key == 'N') {//move to the next photo}

Logical NOT

if (! valid) {//print errors and don't submit form}

Page 39: ACM  365

ACM 262 Course Notes

Adding Logic

if (dayOfWeek == 'Friday') {var fridayCash = prompt('How much money can you

spend?', '');if (fridayCash >= 50) {alert('You should go out to a dinner and a movie.');} else if (fridayCash >= 35) {alert('You should go out to a fine meal.');} else if (fridayCash >= 12) {alert('You should go see a movie.');} else {alert('Looks like you'll be watching TV.');}

}

Page 40: ACM  365

ACM 262 Course Notes

Adding Logic

Page 41: ACM  365

ACM 262 Course Notes

Adding Logic

while (condition) {// javascript to repeat}

Page 42: ACM  365

ACM 262 Course Notes

Adding Logic

var num = 1;while (num <= 5) {document.write('Number ' + num + '<br>');num = num + 1;}

document.write('Number 1 <br>');document.write('Number 2 <br>');document.write('Number 3 <br>');document.write('Number 4 <br>');document.write('Number 5 <br>');

Page 43: ACM  365

ACM 262 Course Notes

Adding Logic

Page 44: ACM  365

ACM 262 Course Notes

Adding Logic

Page 45: ACM  365

ACM 262 Course Notes

Adding Logic

do {// javascript to repeat} while (condition) ;

do {var luckyNumber = prompt('What is your lucky

number?','');luckyNumber = parseInt(luckyNumber, 10);} while (isNaN(luckyNumber));

Page 46: ACM  365

ACM 262 Course Notes

Adding Logic

var num = 6;do{document.write('Number ' + num + '<br>');num = num + 1;} while (num <= 5)

var num = 6;while (num <= 5) {document.write('Number ' + num + '<br>');num = num + 1;}

Page 47: ACM  365

ACM 262 Course Notes

Adding Logic

Page 48: ACM  365

ACM 262 Course Notes

Functions

Page 49: ACM  365

ACM 262 Course Notes

Functions

Page 50: ACM  365

ACM 262 Course Notes

Functions

Page 51: ACM  365

ACM 262 Course Notes

Functions

Page 52: ACM  365

ACM 262 Course Notes

Functions

Page 53: ACM  365

ACM 262 Course Notes

Assignment

1) Write a function that takes two numbers as parameters and writes the sum of the variables into the document with document.write.

Prompt for the user to enter 2 numbers. After the prompts, display the sum on the

webpage by calling the function.

2) Do the same thing with a function that returns the sum.