Exam 2

I. Short Answers

<INPUT TYPE="button" VALUE="Click for Good Grade"
     ONCLICK="alert('So far so good!');">

<INPUT TYPE="button" VALUE="Click for Good Grade"
     ONCLICK="document.TestForm2.Grade.value='A';">

<INPUT TYPE="button" VALUE="Click for More Points"
     ONCLICK="document.TestForm3.Score.value = 
     parseFloat(document.TestForm3.Score.value) + 10;">

<INPUT TYPE="button" VALUE="Show Heads"
     ONCLICK="document.images.coin.src = 
	 'http://www.dickinson.edu/~cs131/heads.gif';">

<TABLE> 
<TR> 
  <TD> Mike </TD>  <TD> Red </TD>  <TD> Dogs </TD> 
</TR>
<TR> 
  <TD> Kate </TD>  <TD> Blue </TD>  <TD> Cats </TD> 
</TR>
</TABLE>


II. Understanding JavaScript

function test1(x) {
   var r;
   r = 0;
   if (x < 5) {
      r = 1;
      }
   if (x > 20) {
      r = 2;
      }	
   if (x <= 10) {
      r = 3;
      }
   return r;
 }

a) test1(25) = 2
b) test1(8) = 3
c) test1(3) = 3
d) test1(12) = 0


function test2(x, y) {							
   var r;
   r = 0;
   if (x > 10) {
      if (y <= 5) {
         r = 1;
         }
      else {
         r = 2;
         }
      }
   else {
      r = 3;
      }
   return r;
}

a) test2(7, 3) = 3
b) test2(11, 2) = 1
c) test2(20, 7) = 2


var count;
count = 2;
while (count <= 6) {
  document.write(count + "<BR>");
  count = count + Math.floor(count / 2);
}
document.write(count);

2
3
4
6
9


III. Writing JavaScript

function canIBePresident(age, country) {
    if (age >= 35 && country == "USA") {
        alert("Yes, you can be president!");
        }
    else {
        alert("No, you can not be president!");
        }
    }

function flipTillHeads() {
    var coin1, coin2;
    coin1 = "tails";
    coin2 = "tails";
    while (coin1 == "tails" || coin2 == "tails") {
        coin1 = randomOneOf(["heads","tails"]);
        coin2 = randomOneOf(["heads","tails"]);
        document.FlipForm.count.value = 
              parseFloat(document.FlipForm.count.value) + 1;
        }
    }