Hello World Small
Hello World Small
Hello World Small
You should see the words, “Hello World.” appear in a new web page.
If you double-click the error, you should see a more descriptive error message:
Because of the way the “Javascript Testbed” works, subtract 3 from the line number that
your browser complains about.
Dynamic Programming
You might have noticed that our last program wasn’t very flexible. If you wanted to say
“Hello, Web” you’d have to change the code. Instead, try this.
Exercise 2
Write a program to:
1) Ask the user their favourite meal
2) Ask the user their favourite day
3) Use their responses to write a sentence like “I like hamburgers on sunday,
too!”
When you’re done, go to the end of this document for the answer.
Making Decisions
Even though your last program was friendly, you might not always agree with the user.
Background
- Javascript considers certain things “true,” while it considers others “false.”
- “1 is 1” is a true statement. In Javascript, we write this as “1 == 1”. That’s
with two equals signs. We use a single equals sign to put information into
variables, but two equals signs to test things.
- “1 is 2” is a false statement.
- ‘day == “monday"’ (from the example) is true only if the user really typed
“monday.”
A common trap
What if we wanted to respond differently to different days of the week? A first attempt
might look like this:
Notice that we’ve written a catch-all “else statement” as a default response. However,
when we run the program and type “monday", we get:
We solve this problem with an “if-else ladder.” If-else ladders guarantee that Javascript
only executes ONE of the chunks of code. They look like this: if(…) else if (…) [many
else ifs] else (…).
Verbally, we would say: “If they typed ‘monday', then respond appropriately. Otherwise,
if they typed ‘friday', then respond appropriately. Otherwise, respond vaguely.”
Exercise 3
Write a program to translate English days of the week to their French equivalent:
1) Ask for a day of the week
2) Convert that to the French equivalent. From Sunday to Saturday, the French
names for days are: Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, and
Samedi.
3) Print the French version to the screen.
When you’re done, go to the end of this document for the answer.
Repeating Yourself
Our last program was useful, but cumbersome to use a lot. How do we fix that?
It asks you 10 times, or until you type “quit”: whichever comes first. If this example
does not make it clear, the ‘*’ symbol is Javascript’s multiply command.
Background
“For loops” are complex, so I’ll talk about them in 3 parts:
1) Part A: Javascript runs this part of the for-loop before anything else. To deal with
part A specifically, imagine that we wrote “var counter = 0;” on a line of its own.
What would that do? It would create a variable (called “counter”) and set it to
zero. An important point is that we can use this variable anywhere in the
following block of code. For example:
2) Part B: Javascript runs this part before each run of the loop. It’s often called “the
test.” Do you remember when I said “Javascript considers certain things true,
while it considers others false?” Well, if this test is true (ie: 0 < 10, 9 < 10, or
counter < 10 when the variable ‘counter’ is really less than 10,) then Javascript
executes the code between the curly braces. Otherwise, it stops running the for
loop.
3) Part C: Javascript runs this part after each loop, but before “part B.” In this case,
we add 1 to our counter. Without this line, ‘counter’ would always be 0 – so
counter would never go above 10 – so our loop would run forever!
1) We can re-run our code (without having to re-type it) with a “for loop.”
2) We can create a variable and assign it at the same time!
3) We can comment about our code if we start the line with “//”.
4) We can break out of a for-loop with the “break” command.
5) We can use Javascript’s “eval” command to evaluate expressions.
6) We can make Javascript write to a new line if we write “<BR>” to the web page.
Exercise 4
Write a program to display the 7-times table (up to 12) on a web page. Let Javascript do
the math, and (of course) do not code each case by hand! When you’re done, go to the
end of this document for the answer.
Delegating Responsibility
When you write programs, you’ll often have to do the same thing several times – but not
necessarily in a loop. Until now, you’ve had to simply re-type the code that does that
thing. There is a better way.
Step 1: Load our Javascript Editor
Like always, make sure that you’ve checked the box, “Assume Javascript?”
then write your responses to the page – each on their own line. You should also see an
alert box appear with your response in it.
Background
Functions are complex, but easier than for loops. The power of functions is simple. You
write them once (see below,) but you can use their “nickname” whenever you want –
without having to re-write the whole function. We only wrote the “getAndWrite”
function once (in the example above,) but we used it twice.
1) Part A: This is simply a nickname for your function so that you can use it in the
rest of your program. You’ve used these nicknames several times already:
“prompt()”, “writeln()”, and “eval()” to name a few. Luckily, the good folks who
wrote Javascript wrote the guts of those functions for you.
2) Part B: This is a variable (or list of variables separated by commas) that let you
give information to your function. For example, pretend that you want to write a
function to add two numbers. You might want to use the function like “var
mySum = addTwoNumbers(1, 10);”. You would then write it:
Once we’ve passed this information in, we can use it like any other variable. We
can even pass this information into other functions (like we’ve done with the
prompt() in the getAndWrite() function above!) We call these input variables
parameters.
3) Part C: When we write functions, Javascript lets us return information back to the
person who used it. For example, our addTwoNumbers() function returns the sum
of the two numbers. Luckily, you’ve used this concept already: the prompt()
function returns whatever the user typed in the prompt box!
1) Functions let us write some code and call it many times – instead of writing the
same code many times.
2) Functions have names to let us refer to them.
3) Functions can have parameters to let us give information to them.
4) Functions can have return values to give us the result of whatever they did.
5) Javascript has an alert function to pop up a browser “alert” box.
Exercise 5
Write a program to display the “12x12” times-table chart on a web page.
Let Javascript do the math, and (of course) do not code each case by hand! To reduce
repetition, you might consider writing a function to help you out. This function, for
example, might write any single times table to the screen. All you need, then, is to call
the function 12 times. When you’re done, go to the end of this document for the answer.
If Javascript interests you, search the internet for “javascript html programming”. If you
didn’t know this already, you can add Javascript to your web pages with very little
difficulty! If you want to learn how to create a web page, search the internet for “html
tutorial”!
Now that you have a solid foundation in Javascript, you might want to check out the
official Netscape Javascript Reference at
http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm.
If programming (but not necessarily Javascript) interests you, look into one of the other
programming languages out there. “Batch programming” and the “Windows scripting
host” help you automate common computer tasks. The Jscript language in the Windows
Scripting Host is a version of Javascript, so you should have little difficulty learning it.
“Unix shell scripting” helps streamline the Unix or Linux prompt that you might see at
work or your Internet Service Provider. The “Java” and “C++” languages help you
create larger, more complex computer programs. To look at these languages, search the
internet for “windows scripting tutorial,” “java tutorial,” or any other language that
strikes your fancy.
Exercise 2
Three lessons help us here:
1) We get information with a prompt: prompt(“...”,”…”);
2) We store information with a variable: var …;
3) We join information with a “+”.
Exercise 3
Remember that we can test what a user types in with an if (or if-else) statement.
Exercise 4
Remember that we can use a for-loop to repeat something many times.
This code uses a technique called “constants” to help you read (and change) the program.
Constants are variables that you define, but do not change. We write constants in all
uppercase. If you want to change the times-table or how high it goes, this code lets you
change the constant -- not everywhere that you use it.
Exercise 5
Remember that we can use a function to wrap common code into a simple statement.
I’ve introduced a new type of comment, a “block comment,” into this program. In this
case, Javascript ignores everything between ‘/*’ and ‘*/’. It’s a good habit to comment
your functions this descriptively. In the future, you’ll be able to quickly understand the
function without having to read the underlying code!