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.
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
The fundamental data types are
Integers, e.g. 1, 2, 3, -27, 0, etc.,
Strings, e.g. “Spam”, “Eggs”, “Sir Lancelot”, “1” (the last one is a string because it’s surrounded by quotes)
Booleans: true and false. Anything that is not
false,0,null, or"" (the empty string)is considered to be true. That is, only these four values are considered false; everything else is true.
Objects are a structure—a “thing”—that has both properties (things that describe it) and methods (things that it can do). (We don’t talk about objects in PHP, but they are a part of the language.)
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.
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:
> to test for greater than and < to
test for less than. E.g.,
var x = 12; var y = 13; var gt = x > y; var lt = x < y;
Since 12 is not greater than 13, the variable gt is set to false.
Similarly, 12 is less than 13, so lt is now set to true.
== to test for equality (note the two equals signs!) and != to
test for inequality. E.g.,
var the_password = 'funkychicken'; var users_guess = 'boringchicken'; var guessed_right = users_guess == the_password; var guessed_wrong = users_guess != the_password;
Since 'funkychicken' is not equal to 'boringchicken', the variable
guessed_right will be false and the variable guessed_wrong will be
true.
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;
Author: Ryan Timmons
Last Modified: 06 August 2008 15:23:13 PDT
URL: http://uwwebpub.com/