Quiz 4: EECS 101
  1. (5 points) Write a Javascript function named lastZ which turns the color of the web page blue if the last character of the string in the text field document.myform.mystring is the letter "Z".

    function lastZ() {
       var strCopy = document.myform.mystring.value;
       var lastIndex = strCopy.length - 1;
       if ( strCopy.charAt(lastIndex) == "Z") {
           document.bgcolor="blue";
       }
    }
    
  2. (2 points) What is the difference between a global variable and a local variable in JavaScript?

    Global variables are defined outside of function definitions, and are accessible from everywhere in your HTML file. Local variables are defined inside functions, and are only available within the functions in which they are defined.

  3. (2 points) What is the value of the variable X after the following JavaScript code is executed? Explain your answer.

    var X = 7;
    for (i=2; i<5; i++) {
      X += i;
    }
    
    X initially has the value 7. The loop changes the value of i from 2 to 3 to 4, each time adding that value to X. The final value of X is 16.

  4. (1 point) What is the value of the variable Y after the following JavaScript code is executed? Explain your answer.

    var X = 4;
    var Z = "6";
    var Y = "X" + Z;
    
    In the last statement, "X" is a literal string which has nothing to do with the variable X. Y has the value "X6".