Practice Exercises for Final Exam


Given two JavaScript assignments X = “15”; and Y = 25; what would be the value of the expressions  X + Y? What about the expression: paserFloat(X) + Y?


Given two JavaScript assignments X = 1.2; and Y = 2.5; what would be the value of the expressions  Math.floor(X + Y)?


Given two JavaScript assignments X = false; and Y = true; what is the value of each of the following Boolean expressions?

  1. X && !Y
  2. X || Y
  3. !X || (X && Y)
  4. X && (X || Y)

Trace the execution of the following code segment by giving the output of the document.write statements:

var x = 5, y = 2;
document.write(x + "," + y + "\n");  
x = x + 2 * y;
y = 3 * x – y;
document.write(x + "," + y + "\n");  
x = 5 * x;
y = y + 4;
document.write(x + "," + y + "\n");  
document.write(x + y);


Suppose that variables roll1 and roll2 represent two dice rolls. Write a sequence of JavaScript statements that result in writing “YES” to the current document if the roll of the two dice adds up to 2 or 7, and writing “NO” otherwise.


Trace the execution of the following code segment by giving the output of the document.write statements:

var x = 1, y = 3;
document.write(x + "," + y + "\n");

if (x + y > 5)
{
    x = 2 * x + 3;
    y = 3 + x + 2;
   
document.write(x + "," + y + "\n");
}
else
{   
    x = 3 * x + 1;
    y = 2 * x - 1;   
   
document.write(x + "," + y + "\n");
}
x = 5 * y;
y = 4 * x;
document.write(x + "," + y + "\n");
 


Trace the execution of the the same code segment as above, but this time replace the first line of the code segment with the following:

var x = 3, y = 3;


Consider the following JavaScript code segment:

if (x > y) {
document.write(“GREATER <br/>”);
y = x;

}

else if (x == y) {
document.write(“EQUAL <br/>”);
x = 100;

}

document.write(“x = “ + x + “, y = “ + y);

For each of the following assignments (prior to the execution of the above code), trace the execution and show what output would be produced.

(a)  x = 0;  y = 5;
(b)  x = 0;  y = -5;
(c)
  x = 9;  y = 9;
(d)
  x = 22;  y = 21;


Write a function addNtoM(N, M) that given two numbers N and M, with N < M, will retrun the sum of all numbers from N through M (including N and M).

Write a sequence of statements to prompt the user for two numbers (stored in variables Var1 and Var2). Then use your function above  to compute the sum from Var1 to Var2 and write the result on the page (using document.write function).

How would you modify the above function, if you could not assume that N < M?

See loop-programming-ex.doc.


Trace the following loop and provide the output generated:

Count = 0;

Last = 10;

Sum = 0;

while (count < last)

{

  document.write("Interation: " + Count + “\n”);

  document.write("Current Sum =" + Sum + "\n");

  if ( (Count % 2) == 0 )     // check to see if Count is even

  {

    Sum = Sum + Count;

  }

     Count = Count + 1;

}

document.write(“DONE! Final Values:” x + "," + y);

 


Trace the following loop and provide the output generated:

x = 1;

y = 15;

while (x < y)

{

  document.write(x + "," + y + “\n”);

  x = x + 1;

  y = y - 3;

}

document.write(“DONE! Final Values:” x + "," + y);

 


Note: Also see the solutions to Information Representation part of Assignment 4.