I have made a calculator in the Code.org App Lab using JS - Caclulator v1.0
In this calculator, every time the equal button is pressed, problemText
, problemText_2
, operator
, and answerText
are placed in a record in the table, arithCalc
. There is a screen in the calculator, which is meant to display the previous calculations. in a textArea
or table. Is there any way to use readRecords()
to obtain the values from the table, and display them in the textArea
in arithDisplay
or in a table in arithDisplay
?
Here is the code:
//ARITHMETIC CALCULATOR
var problemText;
var operator;
var problemText_2 = "";
var answerText;
onEvent("arithButton","click", function() {
setScreen("arithmeticScreen");
});
//button1
onEvent("b1","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "1");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button2
onEvent("b2","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "2");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button3
onEvent("b3","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "3");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button4
onEvent("b4","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "4");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button5
onEvent("b5","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "5");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button6
onEvent("b6","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "6");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button7
onEvent("b7","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "7");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button8
onEvent("b8","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "8");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button9
onEvent("b9","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "9");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
//button0
onEvent("b0","click",function() {
problemText = getText("problemBox");
setProperty("problemBox", "text", problemText + "0");
problemText =(getText("problemBox"));
console.log(problemText);
if (problemText != null) {
setProperty("plusButton","hidden",false);
setProperty("minusButton","hidden",false);
setProperty("multiplyButton","hidden",false);
setProperty("divisionButton","hidden",false);
} else {
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
}
if (problemText.length > 4) {
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
});
onEvent("divisionButton","click", function() {
operator = "÷";
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
setProperty("b0b","hidden",false);
setProperty("b1b","hidden",false);
setProperty("b2b","hidden",false);
setProperty("b3b","hidden",false);
setProperty("b4b","hidden",false);
setProperty("b5b","hidden",false);
setProperty("b6b","hidden",false);
setProperty("b7b","hidden",false);
setProperty("b8b","hidden",false);
setProperty("b9b","hidden",false);
setProperty("problemBox","text",problemText + " " + operator);
problemText = Number(problemText);
problemText_2 = "";
/*createRecord("arithCalc",{num1:problemText, operator:"÷"});*/
});
onEvent("plusButton","click", function() {
operator = "+";
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
setProperty("b0b","hidden",false);
setProperty("b1b","hidden",false);
setProperty("b2b","hidden",false);
setProperty("b3b","hidden",false);
setProperty("b4b","hidden",false);
setProperty("b5b","hidden",false);
setProperty("b6b","hidden",false);
setProperty("b7b","hidden",false);
setProperty("b8b","hidden",false);
setProperty("b9b","hidden",false);
setProperty("problemBox","text",problemText + " " + operator);
problemText = Number(problemText);
problemText_2 = "";
/*createRecord("arithCalc",{num1:problemText, operator:"÷"});*/
});
onEvent("multiplyButton","click", function() {
operator = "×";
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
setProperty("b0b","hidden",false);
setProperty("b1b","hidden",false);
setProperty("b2b","hidden",false);
setProperty("b3b","hidden",false);
setProperty("b4b","hidden",false);
setProperty("b5b","hidden",false);
setProperty("b6b","hidden",false);
setProperty("b7b","hidden",false);
setProperty("b8b","hidden",false);
setProperty("b9b","hidden",false);
setProperty("problemBox","text",problemText + " " + operator);
problemText = Number(problemText);
problemText_2 = "";
/*createRecord("arithCalc",{num1:problemText, operator:"÷"});*/
});
onEvent("minusButton","click", function() {
operator = "-";
setProperty("plusButton","hidden",true);
setProperty("minusButton","hidden",true);
setProperty("multiplyButton","hidden",true);
setProperty("divisionButton","hidden",true);
setProperty("b0b","hidden",false);
setProperty("b1b","hidden",false);
setProperty("b2b","hidden",false);
setProperty("b3b","hidden",false);
setProperty("b4b","hidden",false);
setProperty("b5b","hidden",false);
setProperty("b6b","hidden",false);
setProperty("b7b","hidden",false);
setProperty("b8b","hidden",false);
setProperty("b9b","hidden",false);
setProperty("problemBox","text",problemText + " " + operator);
problemText = Number(problemText);
problemText_2 = String("");
/*createRecord("arithCalc",{num1:problemText, operator:"÷"});*/
});
onEvent("b0b","click",function() {
problemText_2 = problemText_2 + String(0);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b1b","click",function() {
problemText_2 = problemText_2 + String(1);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b2b","click",function() {
problemText_2 = problemText_2 + String(2);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b3b","click",function() {
problemText_2 = problemText_2 + String(3);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b4b","click",function() {
problemText_2 = problemText_2 + String(4);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b5b","click",function() {
problemText_2 = problemText_2 + String(5);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b6b","click",function() {
problemText_2 = problemText_2 + String(6);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b7b","click",function() {
problemText_2 = problemText_2 + String(7);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b8b","click",function() {
problemText_2 = problemText_2 + String(8);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
onEvent("b9b","click",function() {
problemText_2 = problemText_2 + String(9);
setProperty("problemBox", "text", problemText + " " + operator + " " + problemText_2);
console.log(problemText_2.length);
problemText_2 = problemText_2;
});
setInterval(function() {
if (problemText_2.length > 4) {
setProperty("b0b","hidden",true);
setProperty("b1b","hidden",true);
setProperty("b2b","hidden",true);
setProperty("b3b","hidden",true);
setProperty("b4b","hidden",true);
setProperty("b5b","hidden",true);
setProperty("b6b","hidden",true);
setProperty("b7b","hidden",true);
setProperty("b8b","hidden",true);
setProperty("b9b","hidden",true);
setProperty("b0","hidden",true);
setProperty("b1","hidden",true);
setProperty("b2","hidden",true);
setProperty("b3","hidden",true);
setProperty("b4","hidden",true);
setProperty("b5","hidden",true);
setProperty("b6","hidden",true);
setProperty("b7","hidden",true);
setProperty("b8","hidden",true);
setProperty("b9","hidden",true);
}
},1);
onEvent("homeButton","click", function() {
setScreen("startScreen");
});
onEvent("equalButton","click", function() {
if (operator === "÷") {
problemText = Number(problemText);
problemText_2 = Number(problemText_2);
answerText = (problemText/problemText_2).toFixed(5);
} else if (operator === "×") {
problemText = Number(problemText);
problemText_2 = Number(problemText_2);
answerText = problemText*problemText_2;
} else if (operator === "+") {
problemText = Number(problemText);
problemText_2 = Number(problemText_2);
answerText = problemText + problemText_2;
} else if (operator === "-") {
problemText = Number(problemText);
problemText_2 = Number(problemText_2);
answerText = problemText - problemText_2;
}
setText("answerBox",String(answerText));
createRecord("arithCalc",{num1:problemText, operator:operator, num2:problemText_2, result:answerText}, function() {
setProperty("b0b","hidden",true);
setProperty("b1b","hidden",true);
setProperty("b2b","hidden",true);
setProperty("b3b","hidden",true);
setProperty("b4b","hidden",true);
setProperty("b5b","hidden",true);
setProperty("b6b","hidden",true);
setProperty("b7b","hidden",true);
setProperty("b8b","hidden",true);
setProperty("b9b","hidden",true);
setProperty("b0","hidden",false);
setProperty("b1","hidden",false);
setProperty("b2","hidden",false);
setProperty("b3","hidden",false);
setProperty("b4","hidden",false);
setProperty("b5","hidden",false);
setProperty("b6","hidden",false);
setProperty("b7","hidden",false);
setProperty("b8","hidden",false);
setProperty("b9","hidden",false);
problemText = undefined;
setProperty("problemBox","text","");
operator = undefined;
problemText_2 = "";
answerText = undefined;
setText("answerBox","");
});
});
onEvent("clearButton","click",function() {
setProperty("b0b","hidden",true);
setProperty("b1b","hidden",true);
setProperty("b2b","hidden",true);
setProperty("b3b","hidden",true);
setProperty("b4b","hidden",true);
setProperty("b5b","hidden",true);
setProperty("b6b","hidden",true);
setProperty("b7b","hidden",true);
setProperty("b8b","hidden",true);
setProperty("b9b","hidden",true);
setProperty("b0","hidden",false);
setProperty("b1","hidden",false);
setProperty("b2","hidden",false);
setProperty("b3","hidden",false);
setProperty("b4","hidden",false);
setProperty("b5","hidden",false);
setProperty("b6","hidden",false);
setProperty("b7","hidden",false);
setProperty("b8","hidden",false);
setProperty("b9","hidden",false);
problemText = undefined;
setProperty("problemBox","text","");
operator = undefined;
problemText_2 = "";
answerText = undefined;
setText("answerBox","");
});
onEvent("previousResults","click",function() {
setScreen("displayArithmetic");
});
//ARITHMETIC CALCULATOR
//DISPLAY ARITHMETIC RESULTS
onEvent("homeButton_pc","click",function() {
setScreen("startScreen");
});
//DISPLAY ARITHMETIC RESULTS
Is
arithDisplay
a variable? It is not displayed anywhere, or I could not find it. Please reduce your code size down to what is important.that should accomplish what I think you want. Also, instead of writing
setProperty()
you could just usesetText()
which is shorter.