CPSC 101b Classroom Demonstrations: 26 February 2018 <html><head><title> Stan Eisenstat: work.html </title><script> // Does Your Program Work? var response = prompt ("Does your program work (Y or N)?"); if (response == "Y") { alert ("Congratulations!"); } else { response = prompt ("Have you been working more than 1-2 hours (Y or N)?"); if (response == "Y") { alert ("Stop for now and see the instructor for help."); } else { alert ("Keep at it. You should be done soon."); } } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: robust.html </title><script> // Does Your Program Work? (Robust Version) var response= prompt ("Does your program work (Y or N)?"); if (response == "Y") { alert ("Congratulations!"); } else if (response == "N") { response = prompt ("Have you been working more than 1-2 hours (Y or N)?"); if (response == "Y") { alert ("Stop for now and see the instructor or one of the TAs."); } else if (response == "N") { alert ("Keep at it. You should be done soon."); } else { alert ("You did not answer Y or N so I cannot help you."); } } else { alert ("You did not answer Y or N so I cannot help you."); } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: veryrobust.html </title><script> // Does Your Program Work? (Very Robust Version) var response = prompt ("Does your program work (Y or N)?"); if (response == "Y" || response == "y") { alert ("Congratulations!"); } else if (response == "N" || response == "n") { response = prompt ("Have you been working more than 1-2 hours (Y or N)?"); if (response == "Y" || response == "y") { alert ("Stop for now and see the instructor or one of the TAs."); } else if (response == "N" || response == "n") { alert ("Keep at it. You should be done soon."); } else { alert ("You did not answer Y or N so I cannot help you."); } } else { alert ("You did not answer Y or N so I cannot help you."); } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: coins.html </title><script> // Solve the three coins problem using a pan balance: Which of // coins A, B, and C is lighter or heavier. var answer = prompt ("A vs. B: L or E or H?"); if (answer == "L") { // A < B answer = prompt ("B vs. C: L or E or H?"); if (answer == "L") { // B < C document.write ("You lied to me! A < B < C"); } else if (answer == "E") { // B = C document.write ("Coin A is light."); } else { // B > C document.write ("Coin B is heavy."); } } else if (answer == "E") { // A = B answer = prompt ("B vs. C: L or E or H?"); if (answer == "L") { // B < C document.write ("Coin C is heavy."); } else if (answer == "E") { // B = C document.write ("You lied to me! A = B = C"); } else { // B > C document.write ("Coin C is light."); } } else { // A > B answer = prompt ("B vs. C: L or E or H?"); if (answer == "L") { // B < C document.write ("Coin B is light."); } else if (answer == "E") { // B = C document.write ("Coin A is heavy."); } else { // B > C document.write ("You lied to me! C < B < A"); } } </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: smallest1.html </title><script> // Prompt for three values A, B, and C and print smallest s (decision tree) var a, b, c, s a = prompt ("Enter A:"); b = prompt ("Enter B:"); c = prompt ("Enter C:"); if (a > b) { if (b > c) { s = c; } else { s = b; } } else { if (a > c) { s = c; } else { s = a; } } document.write (s + " is the smallest of " + a + ", " + b + ", and " + c); </script></head></html> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <html><head><title> Stan Eisenstat: smallest2.html </title><script> // Prompt for three values A, B, and C and print smallest (compact version) var a, b, c, t; a = prompt ("Enter A:"); b = prompt ("Enter B:"); c = prompt ("Enter C:"); if (a < b) { t = a; } else { t = b; } if (c < t) { t = c; } document.write (t + " is the smallest of " + a + ", " + b + ", and " + c); </script></head></html>