Why am I receiving NaN? What's wrong?

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 :smiley:

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);
	
}

Are you working in p5js or Processing.js? The first one is a JavaScript library that mirrors the functionality of Processing (which is Java with some additions) and the second one is a way to run Processing sketches as web applications without writing them in JS. In the case of p5js you should be writing in JS, in case of Processing.js you should be writing in Java.

Your code contains syntax from both languages, so it’s hard to say what exactly causes the issue.

For example void setup() is Java and function drawNum(txtNum, X) is JS.

if (brojac = 0) and if (brojac = 1) wouldn’t work in both languages. = is the assignment operator and == is the comparison operator, so it should be if (brojac == 0).

1 Like

I completely missed the == syntax.

I thought that function can be used, but I see why it won’t function.
Also, I wanted to use a function because it would be much easier.

Thanks!

I thought that function can be used, but I see why it won’t function.
Also, I wanted to use a function because it would be much easier.

You can use functions in java, just the syntax is different. You have to define the return type and parameter types.

Oh alright!

Also, the problem was in the if statement since drawNum was only for drawing text ON the rectangles.

Thanks again!

dient check on your code and do not know if the problem persists.

NaN means not a number. So I would think that it is not a syntax error but in your formulas.
Actually, a calculator can display NaN too.
It usually happens when you divide by zero…

As in fact it should be a possible outcome of using your calculator, you could use a try() - catch phrase.
so whenever you execute a calculation and the result is not a number (float probably) you can set some routine to display a string in your calculator.