Up until now, each program we looked at had the property that
when it ran, the interpreter executed as many statements as we
put in the program, or possibly fewer once we
introduced if's. That means that we as programmers
have to write very long programs in order to get a lot of work
out of the computer. Now we introduce loops, which
allow short programs to instruct the computer to do a large
amount of work.
alert(1+2+3+4+5+6+7+8+9+10);... but that doesn't scale well to a program that adds up the numbers from 1 to 1,000. Or 1,000,000. Suppose we had variables
var total = 0; and var k = 1;.
we could imagine doing the following over and over:
add k to total and increment k
by 1. If we do this up to and including when k is
10, we will have added up the numbers from 1 to 10. In Javascript,
we execute a block over and over again using a while
statement. Like this:
var total = 0; var k = 1; while(continuation-condition) { \ total = total + k; | Loop k = k + 1; | Body } /The code inside the block is called the loop body, and it's repeated over and over. How long? Well, inside the ( )'s is the continuation condition, a boolean-valued expression, and as long as the continuation condition is true, the body continues to be executed. In this case, we want to loop as long as
k is less than or equal to 10,
which gives us this program:
total all
it likes, but we'll never get k to
equal n + 1 because n equals -1. So
we get an infinite loop. Chrome handles this nicely —
after a short while it just kills the page. In Firefox, this
binds up the whole browser, which is not fun. Moral of the
story is: Watch out for infinite loops!
Math.random() generates a
random number in the range 0 to 1. Obviously we're not talking
just whole numbers! Second, prompt( ) returns the
value null when the user clicks Cancel.
A null value means "nothing".
Make sure you understand this program!
Problem: Modify the program so that it displays
"Better luck next time" rather than "You did it!" if the user
clicks Cancel.Is there an optimum strategy for this game? There is. Figure it out, or look up "binary search" on the web and see if you can figure it out. What's the fewest guesses in which you can guarauntee getting the number right?
eval( ) that takes a
string as its argument and evaluates the string within the
interpreter. So, eval("3 + 4") is 7. Or, more
interestingly, eval(prompt("enter an expression")),
which allows the user to enter an expression and have it
evaluated. Below is a slightly modified version of the number
guessing problem, in which the user's guess is evaluated
with eval( ). If they guess a number, nothing is
changed, since, for example, eval(5) is 5. This
way however, they can enter a guess like this: if you've gotten
to the point where the number is between 3 and 38, you might
enter Math.floor((38 + 3)/2). The program would
evaluate that.
Try it out.
What seemed like an innocent feature to improve the user
experience is, however, a real problem. Now we can cheat.
Run the game and try entering the character N.
Now we can win in one step, every time! What happened?
Since the user can input any expression, he can input
expressions that use the variables within the program's
environment. Here's another fun one: try entering N = 1.
Now not only can we win in one move, we can choose the
"answer". Lots of security flaws result from allowing
arbitrary code that originates from outside a program to run
within the program.
"". It is a string of no characters —
of length zero. Why would I want a string of nothing? Well
... European mathematicians of long ago couldn't see the value
in having a "number" zero? Why would I want a number for
nothing? Actually, we'll see a very common use of the empty
string below, when we create a variable that's initially set to
the empty string so that we can add on to it later.
"foo".length is 3.
If z is a variable of type
string, z.length tells you its length.
"abcd"[0] → "a",
"abcd"[1] → "b",
"abcd"[2] → "c" and
"abcd"[3] → "d".
Note that the last valid index into a string is its length
minus one!
Math.random()
function to generate random passwords of six characters.
Exercise: Modify the program so the user specifies the length of the password.ssh m159999@rona.cs.usna.edu "pwgen -0 -A 8"This should generate a pronoucable 8-character password. It should be much easier to remember!
Every year the secruity conference CanSecWest holds the PWN2OWN contest, where participants try to exploit (take advantage of) bugs in browsers in order to "PWN" a computer whose browser visited their webpage. Essentially, you've pwned a computer if you can get it to execute shell commands of your choosing & preferrably in an administrator/root shell. In 2008, one group took advantage of a bug in Apple's Safari browser's Javascript interpreter to pwn a MacBook Air. In otherwords, the MacBook Air's browser followed a link to this groups evil webpage, and two minutes later they were able to send shell commands to the MacBook Air and have it execute them. Check out this 2008 news article or, if you really want to know, you can look at this whitepaper by the team that created the hack.