Programming for the Web Samples and Explanations

This is still a work in progress. You might find this slightly informative, though.

This is meant to be a sort of “quiz” to prepare you for what to expect with PHP, JavaScript, and Ruby. The Programming workshop is taught with JavaScript, but these examples are applicable to the other languages as well (although they would have to be written with different syntax in the other languages).

This will eventually ferment into the online curriculum for JavaScript.

Variables

Given the following block of JavaScript, what is the final value of the variable foo?

var foo = 17;
var bar = 21  + foo;
var baz = foo + bar;
foo     = foo + bar - baz;

var foo = 17;               // foo = 17
var bar = 21  + foo;        // bar = 21 + 17 = 38
var baz = foo + bar;        // baz = 17 + 38 = 55
foo     = foo + bar - baz;  // foo = 17 + 38 - 55
                            //     = 0

Data Types

The fundamental data types are

Note that JavaScript has another very important data type called a function. We’ll come back to functions in a later quiz question.

An important concept of these data types is to recognize how variables’ data types are converted after certain operations. All of our languages feature dynamic typing (or type juggling) in that a variable’s type may change depending on how it is used. Generally this is a “lowest common denominator” approach.

Booleans and Comparisons

We don’t always want the computer to execute the same thing, so we have to have a way of determining whether or not certain conditions are met. We might not want to submit to the database if the user entered the wrong password, for example.

Central to this idea is that of a comparison. Essentially we want to compare a variable against a known value. We have built-in comparison operations that allow us to compare certain values against one another. The result of a comparison operation is always a boolean: a true or false.

We have the following comparison operations:

What are the types of the following variables after this code has executed?

var v1 = true;
var v2 = 'true';
var v3 = v1 && v2;
var v4 = v1 || v2;
 
var v5 = 'false';
var v6 = false;
var v7 = v5 && v6;
var v8 = v5 || v6;
 
var v1 = '6';
var v1 = fifth + sixth;

Arrays

List Arrays

Dictionary Arrays

If/Else

Loops

Loops 1: While

Loops 2: For

Loops 3: For In

Functions

Functions Without Returned Values

Functions With Returned Values

Objects


Author:  Ryan Timmons
Last Modified:  06 August 2008 15:23:13 PDT
URL:  http://uwwebpub.com/

See Original Markdown