26
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #4 : JAVASCRIPT By Wendy Sharpe 1

CMPT 100 : introduction to computing tutorial #4 : javascript

  • Upload
    luigi

  • View
    58

  • Download
    0

Embed Size (px)

DESCRIPTION

CMPT 100 : introduction to computing tutorial #4 : javascript. By Wendy Sharpe. before we get started . . . If you have not been to a tutorial at all then you must catch up on your own time Log onto the lab computer (in Windows) Create a folder for this tutorial on H:// drive - PowerPoint PPT Presentation

Citation preview

Page 1: CMPT 100 : introduction to computing tutorial #4 : javascript

CMPT 100 : INTRODUCTION TO COMPUTING

TUTORIAL #4 : JAVASCRIPT

By Wendy Sharpe1

Page 2: CMPT 100 : introduction to computing tutorial #4 : javascript

BEFORE WE GET STARTED . . . If you have not been to a tutorial at all

then you must catch up on your own time

Log onto the lab computer (in Windows) Create a folder for this tutorial on H:// drive Log onto Moodle and open tutorial 4 notes

Click on oldmac.html link and save as to your newly created folder

Open Notepad++ Start – All Programs – Course Specific

Applications – Notepad++ We won’t be using Kompozer but you can use it

in conjunction with Notepad++2

Page 3: CMPT 100 : introduction to computing tutorial #4 : javascript

TUTORIAL 4 OVERVIEW Start with an algorithm JavaScript variables Using the document.prompt() function Using the document.write() function String concatenation Background colour Debugging JavaScript

3

Page 4: CMPT 100 : introduction to computing tutorial #4 : javascript

THE OLD MAC ALGORITHM4

Page 5: CMPT 100 : introduction to computing tutorial #4 : javascript

WHAT CHANGES IN THE SONG? Write the title Write the first line Write second line

Write “animal” Write second line pt 2

Repeat 3 with “sound” x 2 Write last line

But where do the animals and sounds come from? 5

Page 6: CMPT 100 : introduction to computing tutorial #4 : javascript

REFINING OUR ALGORITHM Write the title

Prompt for animal Get animal Prompt for sound Get sound Prompt for colour Get colour

Write the first line Write second line

Write “animal” Write second line pt 2

Repeat 3 with “sound” x 2 Write last line 6

Page 7: CMPT 100 : introduction to computing tutorial #4 : javascript

WHERE DO YOU PUT THE SCRIPT? If you want the JavaScript to load before the

rest of the page is needs to be in the <head></head> section But a script will work in either head or body

section of your page Try both to see how they work

If you want the JavaScript to load before the rest of the page is needs to be in the <head></head> section

We’re going to move our Old MacDonald script template from the body section to the head section

7

Page 8: CMPT 100 : introduction to computing tutorial #4 : javascript

JAVASCRIPT VARIABLES8

Page 9: CMPT 100 : introduction to computing tutorial #4 : javascript

NAMING JAVASCRIPT VARIABLES JavaScript variables are used to hold values

or expressions var animal

Rules for variable names: Case sensitive Variables must begin with a letter or the

underscore _ character Our variables in this script will be initialized

to an empty string “” You will get an error if you don’t initialize the

variable to the empty string, and then attempt to concatenate another string 9

Page 10: CMPT 100 : introduction to computing tutorial #4 : javascript

DOCUMENT PROMPT() FUNCTION

10

Page 11: CMPT 100 : introduction to computing tutorial #4 : javascript

DOCUMENT PROMPT() FUNCTION A command that’s already been programmed

into JavaScript You need to tell the computer that you want to

use it

General structure:prompt(“this is your physical prompt text”,

“default”);

Good programming practice is to always leave a default value in your prompt

Use = to assign the value of the prompt to the variable that you are prompting the user for E.g., animal = prompt("Enter a kind of animal","duck");

11

Page 12: CMPT 100 : introduction to computing tutorial #4 : javascript

NOW UPDATE YOUR OLDMAC SCRIPT

Create the three variables

Write the three prompts for the variables

12

Page 13: CMPT 100 : introduction to computing tutorial #4 : javascript

DOCUMENT WRITE() FUNCTION

13

Page 14: CMPT 100 : introduction to computing tutorial #4 : javascript

DOCUMENT WRITE() FUNCTION Most basic JavaScript function

Prints specified text to the screen

If you want to combine commands, use the . E.g., document.write(“The stuff in here is called

an argument or arguments”); Arguments can be html tags

Now, put a header of size one inside a write() function

For additional reading: http://www.mediacollege.com/internet/javascript/basic/document-write.html 14

Page 15: CMPT 100 : introduction to computing tutorial #4 : javascript

STRING CONCATENATION USING DOCUMENT.WRITE();

15

Page 16: CMPT 100 : introduction to computing tutorial #4 : javascript

USING VARIABLES, CONCATENATING STRINGS, AND WRITING TO THE SCREEN use + to put two strings together, or a string

and a variable do not put “” around a variable Watch where you put spaces between strings

and variables E.g., var adjective = “fantastical”;document.write(“My string is ”+adjective+“ and

formatted beautifully <br />”);

Now, update the rest of the Old MacDonald song

If you think you’ve got this, open your html document in a web browser

16

Page 17: CMPT 100 : introduction to computing tutorial #4 : javascript

CHANGING THE BACKGROUND COLOUR . . . OR COLOR

17

Page 18: CMPT 100 : introduction to computing tutorial #4 : javascript

USING BGCOLOR() FUNCTION Use with the .

E.g., document.bgColor() = color; Note the American English spelling of colour, and

it will not work if you spell its name incorrectly

Now, change the bgColor = myVariable in your Old Mac script

18

Page 19: CMPT 100 : introduction to computing tutorial #4 : javascript

DEBUGGING JAVASCRIPT19

Page 20: CMPT 100 : introduction to computing tutorial #4 : javascript

SCRIPT NOT WORKING? Common places to look for errors

Did you spell variables correctly? American English spelling of words like color Are all semi-colons where they need to be? Use syntax highlighting to find errors Did you concatenate your strings properly?

+ in the right places Proper use of double quotation marks

Using the error console in Firefox to find problems with your script Tools – Error Console

Tip: clear the error console, then force refresh Errors option will give you information about

what line of code has the problem 20

Page 21: CMPT 100 : introduction to computing tutorial #4 : javascript

FINISHING THE TUTORIAL EXERCISE

21

Page 22: CMPT 100 : introduction to computing tutorial #4 : javascript

TUTORIAL EXERCISE Add a second verse to the song, with a new

set of animals from the user.  Create new prompts for the second set of animals and use document.write to display the second verse.

Create a new document.  Create three prompts to request some information from the user: Their name Their favorite color A number between 1 and 10

Use the information from the user to create a story.  Use document.write to display the story.

22

Page 23: CMPT 100 : introduction to computing tutorial #4 : javascript

POINT TO PONDER FOR NEXT WEEK’S TUTORIAL

23

Page 24: CMPT 100 : introduction to computing tutorial #4 : javascript

A QUESTION TO PONDER FOR NEXT WEEK’S TUTORIAL . . .

What happens to the Old Mac song if there’s an elephant? Or an aardvark?

24

Page 25: CMPT 100 : introduction to computing tutorial #4 : javascript

AN ALGORITHM FOR AN ELEPHANT OR AN AARDVARK

If first character of variable name is = “a” || “e” || “i” || “u”Write “an”

elsewrite “a”

25

Page 26: CMPT 100 : introduction to computing tutorial #4 : javascript

QUESTIONS?26