Java Script Quiz #3:

 

Question #1: Follow the directions in the code.  Note there is an extra credit component.

 

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Password question</title>

</head>

<body>

<h1>Password test!</h1>

<script type="text/javascript">

<!--

var correctPassword;

var thePassword="student";

correctPassword=prompt("Please enter password to view this page!","");

//You need to insert a IF statement here to check to see if the //password is correct.  If it is not correct you want to put out an //alert.  If it is correct then you want to go to a

//window.location that is my home page.

//To get extra credit: you should loop and give the user three chances //to get the password correct.  If they get it you should go to my //page, if they do not you should tell them access is denied.  Each //time they try and get it wrong, you should put out a message on the //form or an alert

//-->

</script>

</body>

</html>

 

Question #2:  Fix the math facts program (you can use mathfor1x.html, mathfor2x.html or mathfor2ax.html) so that if the student gives a wrong answer, a while loop will stay at that problem giving an incorrect message until the user enters the correct answer.

 

Question #3: I want to fix the endless loop program so it loops through 5 times and then shows a particular picture (your choice). You need to fix it and get it working.  The images can be downloaded from:

 

http://www.pgrocer.net/Cis44/HTML/javascript/path.jpg

http://www.pgrocer.net/Cis44/HTML/javascript/house.jpg

http://www.pgrocer.net/Cis44/HTML/javascript/houseusd.jpg

http://www.pgrocer.net/Cis44/HTML/javascript/snow.jpg

http://www.pgrocer.net/Cis44/HTML/javascript/tree.jpg

http://www.pgrocer.net/Cis44/HTML/javascript/street.jpg

 

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Endless loop</title>

<script type="text/javascript">

<!--

// I am establishing an array of 6 elements called fstArray.  The array

//will have an index that can contain values 0, 1, 2, 3, 4, 5

//(therefore the 6 elements). The source for each element in the array

//is a different picture. Note that the value of the array is that each

// element in the array has the same name and by varying the subscript

//I can point to different elements.  This makes it easy to loop

//through the elements in the array by changing the index that points

//to the different elements.  fstArray[0] gets me the first image,

//fstArray[2] gets me the // second image etc.  Be sure to read about

//arrays in a text or tutorial! Look at my examples!

  fstArray = new Array(6);

  fstArray[0]=new Image;

  fstArray[0].src="house.jpg";

  fstArray[1]=new Image;

  fstArray[1].src="street.jpg";

  fstArray[2]=new Image;

  fstArray[2].src="path.jpg";

  fstArray[3]=new Image;

  fstArray[3].src="snow.jpg";

  fstArray[4]=new Image;

  fstArray[4].src="tree.jpg";

  fstArray[5]=new Image;

  fstArray[5].src="houseusd.jpg";

  index=0;

  function cycle()

  {

   document.rotate.src=fstArray[index].src;

   index=index+1;

   if (index==6)

   {

    index = 0;

   }

   setTimeout("cycle()",1000);

   }

// setTimeout deals with the amount of time in milliseconds before the

// expression refered to evaluates - in this example before cycle. 

// This means that I have set up an endless loop here because onLoad

// says to do cycle and within cycle I say to do cycle - therefore the

// endless loop - a programming technique to use carefully!

//-->

</script>

</head>

<body onload="cycle();">

<img src="snow.jpg"

    name="rotate"

    width="50%"

    height="50%" />

</body>

</html>

 

 

Question #4:  You should debug the following problem.  When it works, the output looks like the image below.  Please note that the text box contains the sum of the cost for the items checked.

 

 

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Check cost</title>

<script type="text/javascript">

<!--

function whichGift(giftInfo)

 {

  var wkCost = 0;

  if (window.document.giftInfo.pda.checked == true)

        {

        wkCost = 120;

      }

  if (window.document.giftInfo.camera.checked == true)

        {

        wkCost = 500;

      }

  if (window.document.giftInfo.laptop.checked == true)

        {

          wkCost = 1500;

        }

  if (window.document.giftInfo.desktop.checked == true)

        {

          wkCost = 900;

        }

  window.document.giftInfo.theCost = wkCost;

 }

function clearData(giftInfo)

 {

  window.document.giftInfo.pda.checked == false;

  window.document.giftInfo.camera.checked == false;

  window.document.giftInfo.laptop.checked == false;

  window.document.giftInfo.desktop.checked == false;

  window.document.giftInfo.theCost.value = 0;

 }

//-->

</script>

</head>

<body>

<h1>Gifts:</h1>

Check the gifts you want:

<form  action="#" name = "giftInfo">

<input type = "checkbox" name = "pda"> Palm Pilot<br />

<input type = "checkbox" name = "camera"> Digital Camera<br />

<input type = "checkbox" name = "laptop"> Laptop Computer<br />

<input type = "checkbox" name = "desktop"> Desktop Computer<br />

<input type = "button" value = "Determine Cost" onclick = "whichGift(giftInfo);">

<input type = "button" value = "Clear" onClick="clearData(giftInfo);">

<input type = "text" name = "theCost" size="30"><br />

</form>

</body>

</html>

 

Question #5:  Take a page that you have or make up a simple one and when it loads have it show a popup window with some information in the window.  To do this you will have to look for my example and then use it to write your own code.