# Problem with calculator

**URL:** <https://forum.kirupa.com/t/problem-with-calculator/91962>\
**Category:** Uncategorized\
**Created:** [October 11, 2003, 12:28am UTC](https://forum.kirupa.com/t/problem-with-calculator/91962 "2003-10-11T00:28:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![2peachy](https://avatars.discourse-cdn.com/v4/letter/2/9d8465/32.png) [@2peachy](https://forum.kirupa.com/u/2peachy)\
**Post date:** [October 11, 2003, 12:28am UTC](https://forum.kirupa.com/t/problem-with-calculator/91962/1 "2003-10-11T00:28:16Z")

</div>

I am having trouble getting my calculator working… could someone look at it…  
I have been looking at it over and over again…and can not figure out what I did wrong…

I have one mc which is the buttons for the calculator  
on that mc… I have code

on (release) {  
\_parent.keyPressed(this.\_name);  
}

then in the main movie…  
have two layers…  
numbers  
keys… I placed the mc on the stage… then made instances

the code I have on the keys layer…

// start by clearing the display and variables  
clearAll();

function clearAll() {  
display = “0”; // the visible display  
memory = 0; // the buffer  
operation = “none”; // what operation is happening  
newNum = true; // whether to start a new number  
}

// function called by buttons  
function keyPressed(keyName) {

```
// do something different for different keys
switch (keyName) {
case "clear" : // for clear key, start over
	clearAll();
	break;
	
case "plus" : // for any operation, go to operate function
	operate(keyName);
	break;
case "minus" :
	operate(keyName);
	break;
case "multiply" :
	operate(keyName);
	break;
case "divide" :
	operate(keyName);
	break;
case "equals" :
	operate(keyName);
	break;
	
default : // all others are added as part of the number
	if (newNum) { // new numbers replace the display
		display = keyName;
		newNum = false;
		if (display == "0") newNum = true; // don't allow leading 0s
	} else {
		display += keyName; // append a digit
	}
	break;
}

```

}

// perform operation and prepare for next operation  
function operate(keyName) {  
switch (operation) {  
case “none” : // this is the first number  
memory = parseFloat(display); // set memory to that number  
break;

```
case "plus" : // perform operation with memory and new number
	memory += parseFloat(display);
	break;
case "minus" :
	memory -= parseFloat(display);
	break;
case "multiply" :
	memory *= parseFloat(display);
	break;
case "divide" :
	memory /= parseFloat(display);
	break;
}

// equals operation is like a clear, but results are displayed
if (keyName == "equals") {
	operation = "none";
} else {
	operation = keyName; // remember this operation for next time
}

display = memory.toString(); // display result
newNum = true; // prepare for next number

```

}

I would post the fla… but guess it is too big for in here…
