Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts

    How to make calculator's actions continuous?

    I have a calculator, and I can't make it do continuous actions like:
    when you press a number and press + and 1 and then press = 3 times so the first number is added 3.

    here's my code:

    Code:
    	// here are the basic variables
    	this.current = '0';
    	this.memory = '0';
    	this.action : '';
    	this.max : 8;
    
    	// adds a digit to the end of this.current
    	addDigit: function(dig){
    		if (this.current.length > this.max){
    			return false;
    		}
    		if(eval(this.current) == 0 && this.current.indexOf('.') == -1){
    			this.current = dig;
    		} else {
    			this.current += ''+dig;
    		}
    		this.updateDisplay();
    	},
    
    	// adds a dot to the end of this.current if it doesn't have any
    	addDot: function(){
    		if(this.current.length == 0){
    			this.current = '0.';
    		} else if(this.current.indexOf('.') == -1){
    			this.current = this.current+'.';
    		}
    		this.updateDisplay();
    	},
    
    	// this sets the current action like '+', it is executed on each press of an action button
    	setAction: function(act){
    		this.action=act;
    		this.memory=this.current;
    		this.current = '0';
    	},
    and this.updateDisplay() is the function to update the display with this.current.

    Please help, thanks.
    Last edited by shedokan; 05-29-2009 at 03:40 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    Need to see more of the program to see if this works, but it may be here:
    Code:
    	// this sets the current action like '+', it is executed on each press of an action button
    	setAction: function(act){
    		this.action=act;
    		this.memory=this.current;
    		this.current = '0';
    	},
    You set the 'this.current' variable to zero rather that leave it as the value to be acted upon ???
    with the subsequent presses of the '=' key.

    Just a guess.
    Try adding a few test alert()s to see intermediate values of calculator.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •