Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jshint multistr: true */
- // ==================================================================== //
- // Code to set up the calculator Buttons. You can ignore this section //
- // ==================================================================== //
- // ============ Define HTML elements ================= //
- var scientificCalcHtml = '<FORM ID="Calc"> \
- <TABLE BORDER=4> \
- <TR> \
- <TD> \
- <INPUT TYPE="text" ID="display" VALUE="0"> \
- <br> \
- </TD> \
- </TR> \
- <TR> \
- <TD> \
- <INPUT TYPE="button" ID="one" VALUE=" 1 "> \
- <INPUT TYPE="button" ID="two" VALUE=" 2 "> \
- <INPUT TYPE="button" ID="three" VALUE=" 3 "> \
- <INPUT TYPE="button" ID="times" VALUE=" x "> \
- <br> \
- <INPUT TYPE="button" ID="four" VALUE=" 4 "> \
- <INPUT TYPE="button" ID="five" VALUE=" 5 "> \
- <INPUT TYPE="button" ID="six" VALUE=" 6 "> \
- <INPUT TYPE="button" ID="divide" VALUE=" / "> \
- <br> \
- <INPUT TYPE="button" ID="seven" VALUE=" 7 "> \
- <INPUT TYPE="button" ID="eight" VALUE=" 8 "> \
- <INPUT TYPE="button" ID="nine" VALUE=" 9 "> \
- <INPUT TYPE="button" ID="plus" VALUE=" + "> \
- <br> \
- <INPUT TYPE="button" ID="clear" VALUE=" clr "> \
- <INPUT TYPE="button" ID="zero" VALUE=" 0 "> \
- <INPUT TYPE="button" ID="equals" VALUE=" = "> \
- <INPUT TYPE="button" ID="minus" VALUE=" - "> \
- <br> \
- <br> \
- <!-- Scientific buttons --> \
- <INPUT TYPE="button" ID="square" VALUE= "x^2"> \
- <INPUT TYPE="button" ID="exp" VALUE= " x^y "> \
- <INPUT TYPE="button" ID="sqrt" VALUE= " sqrt"> \
- <INPUT TYPE="button" ID="factorial" VALUE= " ! "> \
- <br> \
- <!-- "Extra" buttons. Students can program these to do anything. --> \
- <INPUT TYPE="button" ID="ex1" VALUE= "ex1"> \
- <INPUT TYPE="button" ID="ex2" VALUE= "ex2"> \
- <INPUT TYPE="button" ID="ex3" VALUE= "ex3"> \
- <!-- "PlusMinus" or Negation button. --> \
- <INPUT TYPE="button" ID="+-" VALUE= "+/-"> \
- </TABLE> \
- </FORM>'
- var testCasesHTML = '<DIV ID="tests"> \
- <TEXTAREA rows="14" cols="105" ID="progressBar" value="No Tests Run So Far"></TEXTAREA> \
- <br> \
- <INPUT TYPE="button" ID="runTests" VALUE="Run Tests"> \
- </TABLE> \
- </DIV>'
- // ============ Define Calculator Button Testcases ================= //
- var testCases = [];
- function RegisterTestcase(fn) {
- testCases.push(fn);
- }
- function TestClear() {
- var testResult = "TEST FAILED [TestClear]: Set Display to 15, Pressed clr. Expected display to say 0.\n";
- SetDisplayValue(15)
- OnPressClear()
- if (GetDisplayValue() === 0) {testResult= "TEST PASSED [TestClear]\n"}
- return testResult
- }
- RegisterTestcase(TestClear)
- function TestNumbers() {
- var testResult = "TEST FAILED [TestNumbers]: Pressed 2, 5, 1, 3, 4, 0, 9, 6, 8, 7. Expected display to say 2513409687.\n";
- // Enter the number 2513409687
- OnPressClear()
- OnPressTwo();
- OnPressFive()
- OnPressOne()
- OnPressThree()
- OnPressFour()
- OnPressZero()
- OnPressNine()
- OnPressSix()
- OnPressEight()
- OnPressSeven()
- // Make sure that number was actually entered into the display.
- if (2513409687 === GetDisplayValue()) {testResult = "TEST PASSED [TestNumbers]\n"}
- return testResult;
- }
- RegisterTestcase(TestNumbers)
- function TestMultiply() {
- var testResult = "TEST FAILED [TestMultiply]: Pressed clr, 6, x, 4, =. Expected display to say 24.\n";
- OnPressClear();
- OnPressSix();
- OnPressTimes();
- OnPressFour();
- OnPressEquals();
- if (24 === GetDisplayValue()) {testResult = "TEST PASSED [TestMultiply]\n"}
- return testResult;
- }
- RegisterTestcase(TestMultiply)
- function TestDivide() {
- var TestName = "[TestDivide]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 6, /, 2, =. Expected display to say 3.\n";
- OnPressClear();
- OnPressSix();
- OnPressDivide();
- OnPressTwo();
- OnPressEquals();
- if (3 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestDivide)
- function TestAdd() {
- var TestName = "[TestAdd]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 6, +, 8, =. Expected display to say 14.\n";
- OnPressClear();
- OnPressSix();
- OnPressPlus();
- OnPressEight();
- OnPressEquals();
- if (14 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestAdd)
- function TestSubtract() {
- var TestName = "[TestSubtract]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, -, 9, =. Expected display to say -4.\n";
- OnPressClear();
- OnPressFive();
- OnPressMinus();
- OnPressNine();
- OnPressEquals();
- if (-4 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestSubtract)
- function TestSquare() {
- var TestName = "[TestSquare]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 7, x^2. Expected display to say 49.\n";
- OnPressClear();
- OnPressSeven();
- OnPressSquare();
- if (49 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestSquare)
- function TestExponent() {
- var TestName = "[TestExponentiation]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, x^y, 3, =. Expected display to say 125.\n";
- OnPressClear();
- OnPressFive();
- OnPressExp();
- OnPressThree();
- OnPressEquals();
- if (125 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestExponent)
- function TestSqrt() {
- var TestName = "[TestSqrt]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, sqrt. Expected display to say 2.\n";
- OnPressClear();
- OnPressFive();
- OnPressSqrt();
- if (2 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestSqrt)
- function TestFactorial() {
- var TestName = "[TestFactorial]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, !. Expected display to say 120.\n";
- OnPressClear();
- OnPressFive();
- OnPressFactorial();
- if (120 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestFactorial)
- function TestPlusMinus() {
- var TestName = "[TestPlusMinus]"
- var testResult = "TEST FAILED " + TestName + ": Pressed clr, 5, +/-. Expected display to say -5.\n";
- OnPressClear();
- OnPressFive();
- OnPressPlusMinus();
- if (-5 === GetDisplayValue()) {testResult = "TEST PASSED " + TestName + "\n"}
- return testResult
- }
- RegisterTestcase(TestPlusMinus)
- // ============ Define Calculator Display Functions ================= //
- function GetDisplayValue() {
- return parseInt(document.getElementById("display").value);
- }
- function SetDisplayValue(value) {
- if (!isNaN(value)) {
- if (value > 0) { value = Math.floor(value) }
- if (value < 0) { value = Math.ceil(value) }
- document.getElementById("display").value = value;
- }
- }
- function SetErrorMessage(value) {
- document.getElementById("display").value = "Error: " + value;
- }
- // ============ Define Testcase Functions ================= //
- function OnRunTests() {
- function appendTestResult(item, index) {
- document.getElementById("progressBar").value += item()
- }
- document.getElementById("progressBar").value = ""
- testCases.forEach(appendTestResult)
- var d = new Date()
- document.getElementById("progressBar").value += "\n"
- document.getElementById("progressBar").value += "Ran " + testCases.length + " testcases at " + d.getHours() % 12 + ":" + d.getMinutes() + ":" + d.getSeconds() + (d.getHours() > 12 ? " PM" : " AM");
- OnPressClear();
- SetDisplayValue(0);
- }
- // ============ Override cycle shell main function ================= //
- function setup (input) {
- var body = document.body;
- body.innerHTML = scientificCalcHtml + '<br>' + testCasesHTML;
- document.getElementById("one").onclick = OnPressOne;
- document.getElementById("two").onclick = OnPressTwo;
- document.getElementById("three").onclick = OnPressThree;
- document.getElementById("four").onclick = OnPressFour;
- document.getElementById("five").onclick = OnPressFive;
- document.getElementById("six").onclick = OnPressSix;
- document.getElementById("seven").onclick = OnPressSeven;
- document.getElementById("eight").onclick = OnPressEight;
- document.getElementById("nine").onclick = OnPressNine;
- document.getElementById("zero").onclick = OnPressZero;
- document.getElementById("clear").onclick = OnPressClear;
- document.getElementById("equals").onclick = OnPressEquals;
- document.getElementById("plus").onclick = OnPressPlus;
- document.getElementById("minus").onclick = OnPressMinus;
- document.getElementById("times").onclick = OnPressTimes;
- document.getElementById("divide").onclick = OnPressDivide;
- document.getElementById("square").onclick = OnPressSquare;
- document.getElementById("sqrt").onclick = OnPressSqrt;
- document.getElementById("factorial").onclick = OnPressFactorial;
- document.getElementById("exp").onclick = OnPressExp;
- document.getElementById("ex1").onclick = OnPressEx1;
- document.getElementById("ex2").onclick = OnPressEx2;
- document.getElementById("ex3").onclick = OnPressEx3;
- document.getElementById("+-").onclick = OnPressPlusMinus;
- //TODO: Enable this.
- document.getElementById("runTests").onclick = OnRunTests;
- }
- setup()
- // ======================================================== //
- // ======================================================== //
- // ============== Your Code Starts Here! ================== //
- // ======================================================== //
- // ======================================================== //
- //
- // Special Key Functions
- //
- function OnPressClear() {
- SetDisplayValue(0)
- }
- function OnPressEquals() {
- }
- //
- // Number Functions
- //
- function OnPressOne() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 1)
- }
- else{
- SetDisplayValue(1)
- }
- }
- function OnPressTwo() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 2)
- }
- else{
- SetDisplayValue(2)
- }
- }
- function OnPressThree() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 3)
- }
- else{
- SetDisplayValue(3)
- }
- }
- function OnPressFour() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 4)
- }
- else{
- SetDisplayValue(4)
- }
- }
- function OnPressFive() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 5)
- }
- else{
- SetDisplayValue(5)
- }
- }
- function OnPressSix() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 6)
- }
- else{
- SetDisplayValue(6)
- }
- }
- function OnPressSeven() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 7)
- }
- else{
- SetDisplayValue(7)
- }
- }
- function OnPressEight() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 8)
- }
- else{
- SetDisplayValue(8)
- }
- }
- function OnPressNine() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 9)
- }
- else{
- SetDisplayValue(9)
- }
- }
- function OnPressZero() {
- if (GetDisplayValue() > 0){
- SetDisplayValue(GetDisplayValue()*10 + 0)
- }
- else{
- SetDisplayValue(0)
- }
- }
- //
- // Arithmetic functions
- //
- function OnPressPlus() {
- }
- function OnPressMinus() {
- }
- function OnPressDivide() {
- }
- function OnPressTimes() {
- }
- //
- // Scientific functions
- //
- function OnPressFactorial() {
- }
- function OnPressSquare() {
- }
- function OnPressExp() {
- }
- function OnPressSqrt() {
- }
- function OnPressEx1() {
- SetDisplayValue (2513409687)
- }
- function OnPressEx2() {
- SetDisplayValue()
- }
- function OnPressEx3() {
- SetDisplayValue()
- }
- function OnPressPlusMinus() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement