please format code with </> button * homework policy * asking questions
Hello! I am very new to processing js and I am in a “special” group in my class (it means I have done programming).
We were making a calculator and the last thing we had to do is to add numbers aside (I guess) and we received an error, NaN? This means that the output is not a number. We had to end the class there.
I wanna know why this is happening. I am hoping I can ask more stuff in the future
Here’s my code:
void setup() {
size(400, 500);
background(255, 255, 255);
// stroke(220, 220, 220);
stroke(0, 0, 0);
noFill();
int brojac = 0;
textSize(50);
numX = 60; // Default
numY = 160;
}
function drawNum(txtNum, X) {
fill(255, 255, 255);
text(txtNum, X, numY);
fill(102, 102, 102);
}
void draw() {
numY = 160;
// fill(220, 220, 220);
fill(102);
rect(16, 100, 80, 80); // 1
drawNum(1, 70, numY);
rect(112, 100, 80, 80); // 2
drawNum(2, 165, numY);
rect(204, 100, 80, 80); // 3
drawNum(3, 260, numY);
rect(304, 100, 80, 80); // -
drawNum("-", 355, numY);
numY = 255;
rect(16, 196, 80, 80); // 4
drawNum(4, 70, numY);
rect(112, 196, 80, 80); // 5
drawNum(5, 165, numY);
rect(204, 196, 80, 80); // 6
drawNum(6, 260, numY);
rect(304, 196, 80, 80); // /
drawNum("/", 355, numY);
numY = 350;
rect(16, 292, 80, 80); // 7
drawNum(7, 70, numY);
rect(112, 292, 80, 80); // 8
drawNum(8, 165, numY);
rect(204, 292, 80, 80); // 9
drawNum(9, 260, numY);
rect(304, 292, 80, 80); // x
drawNum("x", 355, numY);
rect(16, 388, 80, 80); // +
rect(112, 388, 80, 80); // 0
rect(204, 388, 80, 80); // =
rect(304, 388, 80, 80); // C - Clear
fill(0, 0, 0);
if (mousePressed) {
if (mouseX>16 && mouseX <16+80 && mouseY>100 && mouseY<180) {
if (brojac = 0) {
int num = 1;
String numText = String(num);
brojac++;
}
if (brojac = 1) {
int num = num + num*10;
String numText = String(num);
brojac++;
}
} else if (mouseX>96 && mouseX <96+80 && mouseY>160 && mouseY<255) {
num = 2;
String numText = String(num);
}
}
textSize(50);
textAlign(RIGHT);
text(numText, 10, 20, 380, 180);
}