CPSC S101 Classroom Demonstrations: 18 April 2018 <html><head><title> Stan Eisenstat: isbn-loop.html </title><script> // Prompt for first nine digits of ISBN number (without check digit); // print full ISBN number, including check digit (which can be X). var isbn, digits, check, d, i; // Not subtracting 0 keeps ISBN a string isbn = prompt ("Enter nine-digit ISBN number:"); digits = isbn - 0; // Convert ISBN to number check = 0; // Compute sum WITHOUT check digit for (i = 2; i <= 10; i++) { d = digits % 10; digits = (digits - d) / 10; check = check + i * d; } d = 11 - check % 11; d = d % 11; // Just in case c is 11 if (d == 10) { d = "X"; } document.write (isbn + "-" + d); </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: wildlife.html </title><script> // Each month the population of rabbits and foxes changes according to the // following formulae: // // If there are R rabbits and F foxes at the beginning of the month, // then there are // 1.05 * R - .005 * F * R // rabbits and // .95 * F + .001 * R * F // foxes at the end of the month. // // The first term in the formulae represents what would happen to the // population in the absence of the other species: the number of rabbits // would increase since there are no foxes to eat them; the number of foxes // would decrease since there are no rabbits to eat. The second term // represents the effect of interaction between the species: the number of // rabbits will decrease as foxes eat rabbits; the number of foxes will // increase given the increased food supply. // // Prompt for the initial number of rabbits and foxes, and then simulate // population changes for 25 years, printing the population figures at the // start of the simulation and at the end of every year.} var rabbits, foxes; // Number of rabbits and foxes var month, year; // Month and year of simulation rabbits = prompt ("Enter initial number of rabbits: ") - 0; foxes = prompt ("Enter initial number of foxes: ") - 0; document.write ("Start Simulation" + ": #Rabbits = " + rabbits.toFixed(2) + ", #Foxes = " + foxes.toFixed(2) + "<BR>"); for (year = 1; year <= 25; year++) { for (month = 1; month <= 12; month++) { t = 1.05*rabbits - 0.005*foxes*rabbits; foxes = 0.95*foxes + 0.001*rabbits*foxes; rabbits = t; } document.write ("End of Year " + year + ": #Rabbits = " + rabbits.toFixed(2) + ", #Foxes = " + foxes.toFixed(2) + "<BR>"); } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: zeno.html </title><script> // Simulate Zeno's paradox var pAchilles, vAchilles; // Position and velocity of Achilles var pTortoise, vTortoise; // Position and velocity of Tortoise var time; // Elapsed time in race var dt; // Time for A to reach where T is now time = 0; pAchilles = 0; vAchilles = prompt ("Enter speed of Achilles:") - 0; pTortoise = prompt ("Enter position of Tortoise:") - 0; vTortoise = prompt ("Enter speed of Tortoise:") - 0; while (pAchilles < pTortoise) { dt = (pTortoise - pAchilles) / vAchilles; time = time + dt; pAchilles = pTortoise; pTortoise = pTortoise + vTortoise * dt; document.write ("Time = " + time.toFixed(8) + ", Achilles = " + pAchilles.toFixed(8) + ", Tortoise = " + pTortoise.toFixed(8) + "<br>"); } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: 3n+1.html </title><script> // Prompt for a number and play the 3n+1 game n = prompt ("Enter a positive integer:") - 0; while (n != 1) { document.write (n + ", "); if (n % 2 == 0) n = n / 2; else n = 3*n + 1; } document.write (n + "<br>"); </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: guess1.html </title><script> // Generate a number and let the user guess it lower = 0; upper = 10; number = Math.floor (Math.random() * (upper-lower+1)) + lower; alert ("I am thinking of a number between " + lower + " and " + upper); turns = 1; guess = prompt ("What is your guess?") - 0; while (guess != number) { if (guess < number) guess = prompt ("Too low. What is your next guess?") - 0; else guess = prompt ("Too high. What is your next guess?") - 0; turns++; } document.write ("You found " + guess + " in " + turns + " guesses"); </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: guess2.html </title><script> // Guess the number that the user is thinking of lower = 0; upper = 10; alert ("Think of a number between " + lower + " and " + upper); while (lower < upper) { middle = Math.floor ((lower+upper) / 2); guess = prompt ("Is it less than or equal to " + middle + " (Y or N)?"); if (guess == "Y") upper = middle; else if (guess == "N") lower = middle + 1; else alert ("You did not answer Y or N. Please try again."); document.write ("lower = " + lower + ", upper = " + upper + "<BR>"); } document.write ("Your number was " + lower); </script></head></html>