Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.68 KB | None | 0 0
  1. /*jshint multistr: true */
  2.  
  3. // ==================================================================== //
  4. // Code to set up the calculator Buttons. You can ignore this section //
  5. // ==================================================================== //
  6.  
  7. // ============ Define HTML elements ================= //
  8.  
  9. var scientificCalcHtml = '<FORM ID="Calc"> \
  10. <TABLE BORDER=4> \
  11. <TR> \
  12. <TD> \
  13. <INPUT TYPE="text" ID="display" VALUE="0"> \
  14. <br> \
  15. </TD> \
  16. </TR> \
  17. <TR> \
  18. <TD> \
  19. <INPUT TYPE="button" ID="one" VALUE=" 1 "> \
  20. <INPUT TYPE="button" ID="two" VALUE=" 2 "> \
  21. <INPUT TYPE="button" ID="three" VALUE=" 3 "> \
  22. <INPUT TYPE="button" ID="times" VALUE=" x "> \
  23. <br> \
  24. <INPUT TYPE="button" ID="four" VALUE=" 4 "> \
  25. <INPUT TYPE="button" ID="five" VALUE=" 5 "> \
  26. <INPUT TYPE="button" ID="six" VALUE=" 6 "> \
  27. <INPUT TYPE="button" ID="divide" VALUE=" / "> \
  28. <br> \
  29. <INPUT TYPE="button" ID="seven" VALUE=" 7 "> \
  30. <INPUT TYPE="button" ID="eight" VALUE=" 8 "> \
  31. <INPUT TYPE="button" ID="nine" VALUE=" 9 "> \
  32. <INPUT TYPE="button" ID="plus" VALUE=" + "> \
  33. <br> \
  34. <INPUT TYPE="button" ID="clear" VALUE=" clr "> \
  35. <INPUT TYPE="button" ID="zero" VALUE=" 0 "> \
  36. <INPUT TYPE="button" ID="equals" VALUE=" = "> \
  37. <INPUT TYPE="button" ID="minus" VALUE=" - "> \
  38. <br> \
  39. <br> \
  40. <!-- Scientific buttons --> \
  41. <INPUT TYPE="button" ID="square" VALUE= "x^2"> \
  42. <INPUT TYPE="button" ID="exp" VALUE= " x^y "> \
  43. <INPUT TYPE="button" ID="sqrt" VALUE= " sqrt"> \
  44. <INPUT TYPE="button" ID="factorial" VALUE= " ! "> \
  45. <br> \
  46. <!-- "Extra" buttons. Students can program these to do anything. --> \
  47. <INPUT TYPE="button" ID="ex1" VALUE= "ex1"> \
  48. <INPUT TYPE="button" ID="ex2" VALUE= "ex2"> \
  49. <INPUT TYPE="button" ID="ex3" VALUE= "ex3"> \
  50. <!-- "PlusMinus" or Negation button. --> \
  51. <INPUT TYPE="button" ID="+-" VALUE= "+/-"> \
  52. </TABLE> \
  53. </FORM>'
  54.  
  55. var testCasesHTML = '<DIV ID="tests"> \
  56. <TEXTAREA rows="14" cols="105" ID="progressBar" value="No Tests Run So Far"></TEXTAREA> \
  57. <br> \
  58. <INPUT TYPE="button" ID="runTests" VALUE="Run Tests"> \
  59. </TABLE> \
  60. </DIV>'
  61.  
  62. // ============ Define Calculator Button Testcases ================= //
  63. var testCases = [];
  64.  
  65. function RegisterTestcase(fn) {
  66. testCases.push(fn);
  67. }
  68.  
  69. function TestClear() {
  70. var testResult = "TEST FAILED [TestClear]: Set Display to 15, Pressed clr. Expected display to say 0.\n";
  71.  
  72. SetDisplayValue(15)
  73. OnPressClear()
  74. if (GetDisplayValue() === 0) {testResult= "TEST PASSED [TestClear]\n"}
  75.  
  76. return testResult
  77. }
  78.  
  79. RegisterTestcase(TestClear)
  80.  
  81. function TestNumbers() {
  82. var testResult = "TEST FAILED [TestNumbers]: Pressed 2, 5, 1, 3, 4, 0, 9, 6, 8, 7. Expected display to say 2513409687.\n";
  83.  
  84. // Enter the number 2513409687
  85. OnPressClear()
  86. OnPressTwo();
  87. OnPressFive()
  88. OnPressOne()
  89. OnPressThree()
  90. OnPressFour()
  91. OnPressZero()
  92. OnPressNine()
  93. OnPressSix()
  94. OnPressEight()
  95. OnPressSeven()
  96.  
  97. // Make sure that number was actually entered into the display.
  98. if (2513409687 === GetDisplayValue()) {testResult = "TEST PASSED [TestNumbers]\n"}
  99.  
  100. return testResult;
  101. }
  102.  
  103. RegisterTestcase(TestNumbers)
  104.  
  105. function TestMultiply() {
  106. var testResult = "TEST FAILED [TestMultiply]: Pressed clr, 6, x, 4, =. Expected display to say 24.\n";
  107.  
  108. OnPressClear();
  109. OnPressSix();
  110. OnPressTimes();
  111. OnPressFour();
  112. OnPressEquals();
  113.  
  114. if (24 === GetDisplayValue()) {testResult = "TEST PASSED [TestMultiply]\n"}
  115.  
  116. return testResult;
  117. }
  118.  
  119. RegisterTestcase(TestMultiply)
  120.  
  121. function TestDivide() {
  122. var TestName = "[TestDivide]"
  123. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 6, /, 2, =. Expected display to say 3.\n";
  124.  
  125. OnPressClear();
  126. OnPressSix();
  127. OnPressDivide();
  128. OnPressTwo();
  129. OnPressEquals();
  130.  
  131. if (3 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  132.  
  133. return testResult
  134. }
  135.  
  136. RegisterTestcase(TestDivide)
  137.  
  138. function TestAdd() {
  139. var TestName = "[TestAdd]"
  140. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 6, +, 8, =. Expected display to say 14.\n";
  141.  
  142. OnPressClear();
  143. OnPressSix();
  144. OnPressPlus();
  145. OnPressEight();
  146. OnPressEquals();
  147.  
  148. if (14 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  149.  
  150. return testResult
  151. }
  152.  
  153. RegisterTestcase(TestAdd)
  154.  
  155. function TestSubtract() {
  156. var TestName = "[TestSubtract]"
  157. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, -, 9, =. Expected display to say -4.\n";
  158.  
  159. OnPressClear();
  160. OnPressFive();
  161. OnPressMinus();
  162. OnPressNine();
  163. OnPressEquals();
  164.  
  165. if (-4 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  166.  
  167. return testResult
  168. }
  169.  
  170. RegisterTestcase(TestSubtract)
  171.  
  172. function TestSquare() {
  173. var TestName = "[TestSquare]"
  174. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 7, x^2. Expected display to say 49.\n";
  175.  
  176. OnPressClear();
  177. OnPressSeven();
  178. OnPressSquare();
  179.  
  180. if (49 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  181.  
  182. return testResult
  183. }
  184.  
  185. RegisterTestcase(TestSquare)
  186.  
  187. function TestExponent() {
  188. var TestName = "[TestExponentiation]"
  189. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, x^y, 3, =. Expected display to say 125.\n";
  190.  
  191. OnPressClear();
  192. OnPressFive();
  193. OnPressExp();
  194. OnPressThree();
  195. OnPressEquals();
  196.  
  197. if (125 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  198.  
  199. return testResult
  200. }
  201.  
  202. RegisterTestcase(TestExponent)
  203.  
  204. function TestSqrt() {
  205. var TestName = "[TestSqrt]"
  206. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, sqrt. Expected display to say 2.\n";
  207.  
  208. OnPressClear();
  209. OnPressFive();
  210. OnPressSqrt();
  211.  
  212. if (2 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  213.  
  214. return testResult
  215. }
  216.  
  217. RegisterTestcase(TestSqrt)
  218.  
  219. function TestFactorial() {
  220. var TestName = "[TestFactorial]"
  221. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, !. Expected display to say 120.\n";
  222.  
  223. OnPressClear();
  224. OnPressFive();
  225. OnPressFactorial();
  226.  
  227. if (120 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  228.  
  229. return testResult
  230. }
  231.  
  232. RegisterTestcase(TestFactorial)
  233.  
  234. function TestPlusMinus() {
  235. var TestName = "[TestPlusMinus]"
  236. var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, +/-. Expected display to say -5.\n";
  237.  
  238. OnPressClear();
  239. OnPressFive();
  240. OnPressPlusMinus();
  241.  
  242. if (-5 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
  243.  
  244. return testResult
  245. }
  246.  
  247. RegisterTestcase(TestPlusMinus)
  248.  
  249.  
  250. // ============ Define Calculator Display Functions ================= //
  251. function GetDisplayValue() {
  252. return parseInt(document.getElementById("display").value);
  253. }
  254.  
  255. function SetDisplayValue(value) {
  256. if (!isNaN(value)) {
  257. if (value > 0) { value = Math.floor(value) }
  258. if (value < 0) { value = Math.ceil(value) }
  259. document.getElementById("display").value = value;
  260. }
  261. }
  262.  
  263. function SetErrorMessage(value) {
  264. document.getElementById("display").value = "Error: " + value;
  265. }
  266.  
  267. // ============ Define Testcase Functions ================= //
  268. function OnRunTests() {
  269. function appendTestResult(item, index) {
  270. document.getElementById("progressBar").value += item()
  271. }
  272.  
  273. document.getElementById("progressBar").value = ""
  274. testCases.forEach(appendTestResult)
  275. var d = new Date()
  276. document.getElementById("progressBar").value += "\n"
  277. document.getElementById("progressBar").value += "Ran " + testCases.length + " testcases at " + d.getHours() % 12 + ":" + d.getMinutes() + ":" + d.getSeconds() + (d.getHours() > 12 ? " PM" : " AM");
  278. OnPressClear();
  279. SetDisplayValue(0);
  280. }
  281.  
  282. // ============ Override cycle shell main function ================= //
  283.  
  284. function setup (input) {
  285. var body = document.body;
  286.  
  287. body.innerHTML = scientificCalcHtml + '<br>' + testCasesHTML;
  288.  
  289. document.getElementById("one").onclick = OnPressOne;
  290. document.getElementById("two").onclick = OnPressTwo;
  291. document.getElementById("three").onclick = OnPressThree;
  292. document.getElementById("four").onclick = OnPressFour;
  293. document.getElementById("five").onclick = OnPressFive;
  294. document.getElementById("six").onclick = OnPressSix;
  295. document.getElementById("seven").onclick = OnPressSeven;
  296. document.getElementById("eight").onclick = OnPressEight;
  297. document.getElementById("nine").onclick = OnPressNine;
  298. document.getElementById("zero").onclick = OnPressZero;
  299.  
  300. document.getElementById("clear").onclick = OnPressClear;
  301. document.getElementById("equals").onclick = OnPressEquals;
  302.  
  303. document.getElementById("plus").onclick = OnPressPlus;
  304. document.getElementById("minus").onclick = OnPressMinus;
  305. document.getElementById("times").onclick = OnPressTimes;
  306. document.getElementById("divide").onclick = OnPressDivide;
  307.  
  308. document.getElementById("square").onclick = OnPressSquare;
  309. document.getElementById("sqrt").onclick = OnPressSqrt;
  310. document.getElementById("factorial").onclick = OnPressFactorial;
  311. document.getElementById("exp").onclick = OnPressExp;
  312.  
  313. document.getElementById("ex1").onclick = OnPressEx1;
  314. document.getElementById("ex2").onclick = OnPressEx2;
  315. document.getElementById("ex3").onclick = OnPressEx3;
  316. document.getElementById("+-").onclick = OnPressPlusMinus;
  317.  
  318. //TODO: Enable this.
  319. document.getElementById("runTests").onclick = OnRunTests;
  320.  
  321. }
  322.  
  323. setup()
  324.  
  325. // ======================================================== //
  326. // ======================================================== //
  327. // ============== Your Code Starts Here! ================== //
  328. // ======================================================== //
  329. // ======================================================== //
  330.  
  331. //
  332. // Special Key Functions
  333. //
  334.  
  335. function OnPressClear() {
  336. SetDisplayValue(0)
  337. }
  338.  
  339. function OnPressEquals() {
  340. }
  341.  
  342. //
  343. // Number Functions
  344. //
  345.  
  346. function OnPressOne() {
  347. if (GetDisplayValue() > 0){
  348. SetDisplayValue(GetDisplayValue()*10 + 1)
  349. }
  350.  
  351. else{
  352. SetDisplayValue(1)
  353. }
  354. }
  355.  
  356.  
  357.  
  358. function OnPressTwo() {
  359. if (GetDisplayValue() > 0){
  360. SetDisplayValue(GetDisplayValue()*10 + 2)
  361. }
  362.  
  363. else{
  364. SetDisplayValue(2)
  365. }
  366. }
  367.  
  368. function OnPressThree() {
  369. if (GetDisplayValue() > 0){
  370. SetDisplayValue(GetDisplayValue()*10 + 3)
  371. }
  372.  
  373. else{
  374. SetDisplayValue(3)
  375. }
  376.  
  377. }
  378.  
  379. function OnPressFour() {
  380. if (GetDisplayValue() > 0){
  381. SetDisplayValue(GetDisplayValue()*10 + 4)
  382. }
  383.  
  384. else{
  385. SetDisplayValue(4)
  386. }
  387. }
  388.  
  389. function OnPressFive() {
  390. if (GetDisplayValue() > 0){
  391. SetDisplayValue(GetDisplayValue()*10 + 5)
  392. }
  393.  
  394. else{
  395. SetDisplayValue(5)
  396. }
  397.  
  398. }
  399.  
  400. function OnPressSix() {
  401. if (GetDisplayValue() > 0){
  402. SetDisplayValue(GetDisplayValue()*10 + 6)
  403. }
  404.  
  405. else{
  406. SetDisplayValue(6)
  407. }
  408.  
  409. }
  410.  
  411. function OnPressSeven() {
  412. if (GetDisplayValue() > 0){
  413. SetDisplayValue(GetDisplayValue()*10 + 7)
  414. }
  415.  
  416. else{
  417. SetDisplayValue(7)
  418. }
  419.  
  420. }
  421.  
  422. function OnPressEight() {
  423. if (GetDisplayValue() > 0){
  424. SetDisplayValue(GetDisplayValue()*10 + 8)
  425. }
  426.  
  427. else{
  428. SetDisplayValue(8)
  429. }
  430.  
  431. }
  432.  
  433. function OnPressNine() {
  434. if (GetDisplayValue() > 0){
  435. SetDisplayValue(GetDisplayValue()*10 + 9)
  436. }
  437.  
  438. else{
  439. SetDisplayValue(9)
  440. }
  441.  
  442. }
  443.  
  444. function OnPressZero() {
  445. if (GetDisplayValue() > 0){
  446. SetDisplayValue(GetDisplayValue()*10 + 0)
  447. }
  448.  
  449. else{
  450. SetDisplayValue(0)
  451. }
  452.  
  453. }
  454.  
  455. //
  456. // Arithmetic functions
  457. //
  458.  
  459. function OnPressPlus() {
  460. }
  461.  
  462. function OnPressMinus() {
  463. }
  464.  
  465. function OnPressDivide() {
  466. }
  467.  
  468. function OnPressTimes() {
  469. }
  470.  
  471. //
  472. // Scientific functions
  473. //
  474.  
  475. function OnPressFactorial() {
  476. }
  477.  
  478. function OnPressSquare() {
  479. }
  480.  
  481. function OnPressExp() {
  482. }
  483.  
  484. function OnPressSqrt() {
  485. }
  486.  
  487. function OnPressEx1() {
  488.  
  489. SetDisplayValue (2513409687)
  490.  
  491. }
  492.  
  493. function OnPressEx2() {
  494. SetDisplayValue()
  495. }
  496.  
  497. function OnPressEx3() {
  498. SetDisplayValue()
  499. }
  500.  
  501. function OnPressPlusMinus() {
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement