JavaScript V                      

   Contact
   Search
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Messages
   Chat
   MCSE
   Linux
   Cabling   
   ActionScript
   Downloads
   E-Cards   
 
    
    
JavaScript 1 JavaScript 2 JavaScript 3 JavaScript 4 JavaScript 5 JavaScript 6 Active X/ CSS Filters
 
JavaScript - Class Projects

Dice Game 1:
<HTML>
<HEAD>
<TITLE>Switch Excercise</TITLE>
</HEAD>
<BODY bgcolor="ffffff" text="000000">
<SCRIPT Language="JavaScript">
var NRolls = 0;
var Score = 0;
function RollDice() {
for(NRolls = 1; NRolls<=3; NRolls++) {
var Dice = 0;
document.write("Roll Number" +NRolls+ ".<br>");
Dice = Math.floor(1 + Math.random() * 6);
Score = Score + Dice;
switch (Dice) {
case 1:
    document.write("You just rolled a 1!<br>");
    DisplayRandomMessage(); 
    break;
case 2:
    document.write("You just rolled a 2!<br>");
    DisplayRandomMessage(); 
    break;
case 3:
    document.write("You just rolled a 3!<br>");
    DisplayRandomMessage(); 
    break;
case 4:
    document.write("You just rolled a 4!<br>");
    DisplayRandomMessage(); 
    break;
case 5:
    document.write("You just rolled a 5!<br>");
    DisplayRandomMessage(); 
    break;
case 6:
    document.write("You just rolled a 6!<br>");
    DisplayRandomMessage(); 
    break;
default:
    document.write("You must be using funny dice!"); 
} //close switch statement
} //close for loop
document.write("You have rolled a combined total of " +Score+ " during your three rolls!");
if(Score <= 5 || Score >=14)
   document.write("<br>You win!  Congratulations!");
else
   document.write("<br>You loose!  Sorry...");
} //close RollDice() function

function DisplayRandomMessage() {
var message1 = "Displaying message one.";
var message2 = "Displaying message two.";
var message3 = "Displaying message three.";
var message4 = "Displaying message four.";
var message5 = "Displaying message five.";
var message6 = "Displaying message six.";
messagenumber = Math.floor(1 + Math.random() * 8);
switch (messagenumber) {
case 1:
     document.write(message1);
     break;
case 2:
     document.write(message2);
     break; 
case 3:
     document.write(message3);
     break;
case 4:
     document.write(message4);
     break;
case 5:
     document.write(message5);
     break;
case 6:
     document.write(message6);
     break;
default:
     document.write("Wow! I\'m so confused that you don\'t even get a message!");
} //close switch statement
} //close random error message function
</SCRIPT>
<br><br><br>
<center>
<input type="button" value="Roll Dice" onclick="RollDice()">
</center>
<br><br>
<center>
<a href = "dice.html" onclick="RollDice();">Roll Dice - Dynamically Write New Page</A> 
</center>
<center>
<a href = "dice.html" onclick="RollDice();"><img src="myimage.gif" border="0"></A> 
</center>
</BODY>
</HTML>

Dice Game 2:

<HTML>
<HEAD>
<TITLE>Switch Excercise</TITLE>
</HEAD>

<BODY bgcolor="ffffff" text="000000">
<SCRIPT Language="JavaScript">
var NRolls = 0;
var Score = 0;
//Note: Function Below rolls dice in the same browser instance (as new HTML page)
function RollDice() {
document.write("<BODY bgcolor=\"000000\" text=\"ffffff\"><center>"); 
for(NRolls = 1; NRolls<=3; NRolls++) {
var Dice = 0;
document.write("Dice Roll number " +NRolls+ ".");
Dice = Math.floor(1 + Math.random() * 6);
Score = Score + Dice;
switch (Dice) {
case 1:
    document.write("You just rolled a 1!<br>");
    break;
case 2:
    document.write("You just rolled a 2!<br>");
    break;
case 3:
    document.write("You just rolled a 3!<br>");
    break;
case 4:
    document.write("You just rolled a 4!<br>");
    break;
case 5:
    document.write("You just rolled a 5!<br>");
    break;
case 6:
    document.write("You just rolled a 6!<br>");
    break;
default:
    document.write("You must be using funny dice!"); 
} //close switch statement
} //close for loop
document.write("You have rolled a combined total of " +Score+ " during your three rolls!");
if(Score <= 5 || Score >=14)
   document.write("<br>You win!  Congratulations!");
else
   document.write("<br>You loose!  Sorry...");
document.write("</BODY></center>");     
} //close RollDice() function
function RollDiceInNewWindow() {
NewDiceWindow = window.open("Rolling the Dice","","height=200,width=450");
NewDiceWindow.document.write("<BODY bgcolor=\"000000\" text=\"ffffff\"><center>"); 
for(NRolls = 1; NRolls<=3; NRolls++) {
var Dice = 0;
NewDiceWindow.document.write("Dice Roll number " +NRolls+ ".");
Dice = Math.floor(1 + Math.random() * 6);
Score = Score + Dice;
switch (Dice) {
case 1:
    NewDiceWindow.document.write("You just rolled a 1!<br>");
    break;
case 2:
    NewDiceWindow.document.write("You just rolled a 2!<br>");
    break;
case 3:
    NewDiceWindow.document.write("You just rolled a 3!<br>");
    break;
case 4:
    NewDiceWindow.document.write("You just rolled a 4!<br>");
    break;
case 5:
    NewDiceWindow.document.write("You just rolled a 5!<br>");
    break;
case 6:
    NewDiceWindow.document.write("You just rolled a 6!<br>");
    break;
default:
    NewDiceWindow.document.write("You must be using funny dice!"); 
} //close switch statement
} //close for loop
NewDiceWindow.document.write("You have rolled a combined total of " + Score + 
" during your three rolls!");
if(Score <= 5 || Score >=14)
   NewDiceWindow.document.write("<br>You win!  Congratulations!");
else
   NewDiceWindow.document.write("<br>You loose!  Sorry...");
NewDiceWindow.document.write("<br><br><table border=\"5\" align=\"center\"><tr><td>
<center><a href=\"\" onclick=\"self.close();\">CLOSE</a>");
NewDiceWindow.document.write("</center></td></tr></table>");  
NewDiceWindow.document.write("<\BODY></center>");     
} //close RollDiceInNewWindow() function
</SCRIPT>
<br><br><br>
<center>
<input type="button" value="Roll Dice" onclick="RollDice()">
</center>
<br><br>
<center>
<a href = "dice.html" onclick="RollDice();">Roll Dice</A> 
</center>
<br>
<center>
<a href = "dice.html" onclick="RollDice();"><img src="myimage.gif" border="0"></A> 
</center>
<br>
<center>
<input type="button" value="Roll Dice in New Window" onclick="RollDiceInNewWindow()">
</center>
<br><br>
<center>
<a href = "dice.html" onclick="RollDiceInNewWindow();">Roll Dice in New Window</A> 
</center>
<br>
<center>
<a href = "dice.html" onclick="RollDiceInNewWindow();">
<img src="myimage.gif" border="0"></A> 
</center>
</BODY>
</HTML>

Diamond - Using nested for loops and labels:

<HTML>
<HEAD>
<TITLE>Switch Excercise</TITLE>
</HEAD>
<BODY bgcolor="ffffff" text="000000">
<SCRIPT Language="JavaScript">
nextRow:
     for(row=1; row<=5; ++row) {
      document.writeln("<center>");
        for(var column=1; column<=10; ++column){
         if (column > row)
          continue nextRow;
        document.write("* ");
    }//close column loop
   document.write("</center>");
  }//close row loop
nextRow2:
     for(row=1; row<=5; ++row) {
      document.writeln("<center>");
        for(var column=5; column>=1; --column){
         if (column < row)
          continue nextRow2;
        document.write("* ");
    }//close column loop
   document.write("</center>");
  }//close row loop
</SCRIPT>
</BODY>
</HTML>

Displaying a Random Message:

<HTML>
<HEAD>
<TITLE>Switch Excercise</TITLE>
</HEAD>
<BODY bgcolor="ffffff" text="000000">
<SCRIPT Language="JavaScript">
function DisplayRandomMessage() {
var message1 = "Displaying message one.";
var message2 = "Displaying message two.";
var message3 = "Displaying message three.";
var message4 = "Displaying message four.";
var message5 = "Displaying message five.";
var message6 = "Displaying message six.";
messagenumber = Math.floor(1 + Math.random() * 8);
switch (messagenumber) {
case 1:
     document.write(message1);
     break;
case 2:
     document.write(message2);
     break; 
case 3:
     document.write(message3);
     break;
case 4:
     document.write(message4);
     break;
case 5:
     document.write(message5);
     break;
case 6:
     document.write(message6);
     break;
default:
     document.write("Wow! I\'m so confused that you don\'t even get a message!");
} //close switch statement
} //close random error message function
 
</SCRIPT>
<br><br><br><br>
<center>
<a href="" onclick="DisplayRandomMessage();">TEST</a>
</center>
</BODY>
</HTML>