JavaScript Programming Hints

if-else test

<html> <head> <title>Javascript Test</title> </head> <body> <script>

var myVar = 3; // or some other number
var x = 5; // or some other number
var boolVar = true; // or else false
// test line goes here
document.write(myVar);

</script> </body> </html>

Back to tutorial
 
 
 
 

bubble sort

function ShowArray(msg,theList)
{
  for (var i=0; i<theList.length; i++) msg = msg + " " + theList[i];
  document.write(msg);
}

var theList = [6,2,9,5,4,1,3];
var N = theList.length;
ShowArray("initial:",theList);

for (outer=1; outer<N; outer++)
{
  var xchd = false;
  for (var ix=1; ix<N; ix++)
  {
    if (theList[ix-1] <= theList[ix]) continue;
    var tmp = theList[ix-1];
    theList[ix-1] = theList[ix];
    theList[ix] = tmp;
    xchd = true;
  }
  ShowArray("<p>pass "+outer+":",theList);
  if (!xchd) break;
}

Back to tutorial
 
 
 
 
 

selected display

var SelKey = 4;
var LowItem = "1700";
var LessThan = "1812/06";
for (var ix=0; ix<MusicData.length; ix++)
{
  var aLine = MusicData[ix];
  var nitem = ItemOf(aLine,SelKey,"\t");
  if (nitem<LowItem) continue;
  if (nitem<LessThan) document.write(MusicData[ix]+"<p>");
}
Back to tutorial
 
 
 
 
 
 

TicTacToe loops

function ShoGame1() {
  var ix;
  document.write("<table BORDER COLS=3 WIDTH=60 ><tr>");
  for (ix=1; ix<10; ix++) {
    document.write("<td ALIGN=CENTER><a href=\"Javascript:play("
        + ix + ")\">" + ix + "</a></td>");
    if (ix%3 != 0) continue;
    if (ix==9) break;
    document.write("</tr><tr>");}
  document.write("</tr></table>");}

function ShoGame2() {
  var row;
  var col;
  document.write("<table BORDER COLS=3 WIDTH=60 >");
  for (row=0; row<3; row++) {
    document.write("<tr>");
    for (col=0; col<3; col++)
      document.write("<td ALIGN=CENTER><a href=\"Javascript:play("
          + (row*3+col+1) + ")\">" + (row*3+col+1)+ "</a></td>");
    document.write("</tr>");}
  document.write("</tr></table>");}

function play(whom) {} // dummy

ShoGame1();
ShoGame2();

Back to tutorial
 
 
 
 
 
 
 

TicTacToe X/O Display

function ShoGame(node) {
  var ix;
  var ofx;
  document.write("<table BORDER COLS=3 WIDTH=62 ><tr>");
  for (ix=1; ix<10; ix++) {
    document.write("<td ALIGN=CENTER>");
    ofx = node.indexOf(ix);
    if (ofx<0) document.write("<a href=\"Javascript:play("
        + ix + ")\">" + ix + "</a>");
    else if ((ofx&1) ==0) document.write("<b>X</b>");
      else document.write("<b>O</b>");
    document.write("</td>");
    if (ix%3 != 0) continue;
    if (ix==9) break;
    document.write("</tr><tr>");}
  document.write("</tr></table>");}

function play(whom) {} // dummy

ShoGame("52739");

Back to tutorial