CPSC 101b Classroom Demonstrations: 19 February 2018

<html><head>
<title> Stan Eisenstat: hello.html </title>
<script>
  // Hello, world! program
  alert ("Hello, world!");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: farmer1.html </title>
<script>
  // Farmer, fox, goose, and corn
  alert ("Farmer rows goose across river and returns alone.");
  alert ("Farmer rows fox across river and returns with goose.");
  alert ("Farmer rows corn across river and returns alone.");
  alert ("Farmer rows goose across river. Problem solved!");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: farmer2.html </title>
<script>
  // Farmer, fox, goose, and corn
  document.write ("Farmer rows goose across river and returns alone.<br>");
  document.write ("Farmer rows fox across river and returns with goose.<br>");
  document.write ("Farmer rows corn across river and returns alone.<br>");
  document.write ("Farmer rows goose across river.");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: name.html </title>
<script>
  // Hello, name
  var first = "";                       // Declare & initialize vars (optional)
  var last = "";
  last = prompt ("Last name?");
  first = prompt ("First name?");
  document.write ("Hello, " + first + " " + last + "!");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: fancy.html </title>
<script>
  // Fancy version of name.html using string functions
  var last = "", first = "", mi = "";   // Declare & initialize vars (optional)
  var length = 0;

  last  = prompt ("What is your last name?");
  first = prompt ("What is your first name?");
  mi    = prompt ("What is your middle initial?");
  length = last.length + first.length + mi.length;
  alert ("Your name, " + first.toUpperCase() + " " + mi.toUpperCase()
       + ". " + last.toUpperCase() + ", contains " + length + " characters.");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: temp.html </title>
<script>
  // Temperature conversion: Fahrenheit to Celsius
  var fahr, cels;

  // Subtract 0 to ensure that value is treated as number
  fahr = prompt ("What is the temperature (in Fahrenheit)?") - 0;

  cels = 5 * (fahr - 32) / 9;
  document.write (fahr + " deg Fahrenheit = "
		       + cels.toFixed(2) + " deg Celsius");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: convert1.html </title>
<script>
  // Prompt for day and time; print #seconds in month to date
  var day, hour, min, sec;
  var nhour, nmin, nsec;

  // Subtract 0 to ensure that value is treated as number
  day  = prompt ("What is the day of the month?") - 0;
  hour = prompt ("What is the hour of the day (0-23)?") - 0;
  min  = prompt ("What is the minute of the hour?") - 0;
  sec  = prompt ("What is the second of the minute?") - 0;

  nhour = hour + 24 * (day-1);
  nmin  = min  + 60 * nhour;
  nsec  = sec  + 60 * nmin;
  document.write ("day " + day + ", hour " + hour + ", minute " + min +
		  ", second " + sec + " = " + nsec + " seconds");
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: convert2.html </title>
<script>
  // Prompt for #seconds in month to date; print day and time
  var nday, nhour, nmin, nsec;
  var  day,  hour,  min,  sec;

  // Subtract 0 to ensure that value is treated as number
  nsec = prompt ("What is the second of the month?") - 0;
  sec  = nsec  % 60;  nmin  = (nsec  - sec)  / 60;
  min  = nmin  % 60;  nhour = (nmin  - min)  / 60;
  hour = nhour % 24;  nday  = (nhour - hour) / 24;
  day  = nday + 1;
  document.write (nsec + " seconds = day " + day + ", hour " + hour +
		  ", minute " + min + ", second " + sec);
</script></head></html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<html><head>
<title> Stan Eisenstat: isbn.html </title>
<script>
  // Prompt for language, publisher, and book from ISBN number;
  // print full ISBN number, including check digit
  var lang;
  var pub, p1, p2, p3;
  var book, b1, b2, b3, b4, b5;
  var check, c;

  // Subtract 0 to ensure that value is treated as number
  lang = prompt ("Enter one-digit language code:") - 0;
  pub  = prompt ("Enter three-digit publisher code:") - 0;
  book = prompt ("Enter five-digit book number:") - 0;

  p3 = pub % 10;  pub = (pub - p3) / 10;        // pub = p1 p2 p3
  p2 = pub % 10;  pub = (pub - p2) / 10;
  p1 = pub % 10;

  b5 = book % 10;  book = (book - b5) / 10;     // book = b1 b2 b3 b4 b5
  b4 = book % 10;  book = (book - b4) / 10;
  b3 = book % 10;  book = (book - b3) / 10;
  b2 = book % 10;  book = (book - b2) / 10;
  b1 = book % 10;

  check = 10*lang + 9*p1 + 8*p2 + 7*p3          // check + c divisible by 11
	+ 6*b1 + 5*b2 + 4*b3 + 3*b4 + 2*b5;
  c = 11 - check % 11;
  c = c % 11;                                   // Just in case c is 11

  document.write ("ISBN number is " + lang + "-" + p1 + p2 + p3
		  + "-" + b1 + b2 + b3 + b4 + b5 + "-" + c);
</script></head></html>