# This week’s Challenge – To build a Calculator.

I know Windows has a Calculator, but we are going to build our own. This will give you an idea of how real Calculators actually work.

# The Calculator should be able to:

1. Add, Subtract, Multiply and Divide 2 numbers.

2. Be able to work with decimal points (2.5 for example).

3. Display the sum and the result on the screen.

4. Have a “clear” button to reset everything ready for the next calculation.

# Let’s look at the steps that you need for a calculation

1. Clear the variables.

2. Click on the numbers for the first number.

3. Click on the operator you want to use.

4. Click on the numbers for the second number.

5. Click on the equals button.

6. Display the result.

Hint: When you click on the Operator, this indicates we have got the first number and need to start storing the second number. See variable 6.

It sounds like there is a lot to do, but if we are smart with the way we work, there is a lot of code that is very similar and we can copy and paste it and then modify it slightly.

## You will need 6 variables (for all Sprites) to share:

1. The first number in the calculation (NumberOne), not visible.

2. The second number in the calcualtion (NumberTwo), not visible.

3. The complete sum (Question), visible.

4. The operator (+,-,x,/) for the calculation (Operator), not visible.

5. The result of the calculation (Result), visible.

6. A variable to indicate what number we are working with (SecondNumber). not visible.

Hint: When you press clear, all the variables should be set to blank, except the SecondNumber which should be 0

## You will need 17 Sprites, don’t panic! Remember copy and paste is your friend:

11 Number Sprites 0,1,2,3,4,5,6,7,8,9 and the decimal point.

4 Operator Sprite +,-,x and /.

1 Equals (=) Sprite.

1 Clear Sprite.

## Scratch code blocks that will be used:

Operators, we will use the join operator a lot as well as the +,-,x and /.

If Else block with variable 6 to decide what number to update.

set variable blocks – lots of these!

### Hint:

Start by creating the variables.

Once you can get 1 + 1 working, the rest is quite easy.

The answer is 2 (not 11) by the way, just in case.

### Coding Tips:

Here is some of the code that we will be using:

This is the code when you click on a number Sprite, this example is for the number 1, can you see what needs to change if this was the number 2 sprite?

This is the code for the + Sprite

And this is the code in the = Sprite

And finally, the CLR sprite