# Lesson 54 Calculator Project – Null

We left off with a problem. You can only add one-digit numbers. That doesn’t make our calculator very useful since we can do that in our heads. No tools necessary. The problem is that we set it up so that the first number hit becomes num1 and the second number pressed becomes num2. We need to be able to put as many digits as we want into each number.

The first thing we need to do is get rid of num1 = 11. I did that because there was no 11 button, so it was never going to be pushed, but now we might have an 11 as the number being used, so that won’t work. Instead, I’m going to make num1 be empty. Then I can say if it’s empty, put in the number being pressed. In coding terms, emptiness is described with the word null.

So, we start with num1 and num2 equal to null. Then we check to see if they are null and if they are we make them equal to the number pressed. If they are not empty, then we tag on the number being pressed. If num1 is 2 and 3 is pressed, then num1 is 23.

Then we just have the issue of telling the computer when to start making num2. We can’t just have all the digits going into number 1. I solved that problem by simply creating a variable called currentNumber. I set it to 1. Once they press the operator, such as plus, then it changes to 2. I did that by just simply using currentNumber++. That is short hand for currentNumber = currentNumber + 1. Once we hit clear or equals, currentNumber gets set back to 1 to start us over again. When that happens, I set num1 and num2 back to null as well. It’s a reset, time to start fresh.

In the code snippet, you’ll see that I wrote out that part twice. I could have created a little function instead and called it twice. Like this:

function reset() {

num1 = null;

num2 = null;

currentNumber = 1;

}

And then inside the click clear and click enter functions, I would call it by using: reset();

You can see the code that changed below. I changed the variable declarations at the top. I added if and else statements to the number click function. I added that reset code to both the click clear and enter functions.  You can watch this lesson on youtube.