A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Simple question??

  1. #1
    Peace
    Join Date
    Nov 2006
    Posts
    8

    Simple question??

    (Sorry my english )

    i want do something like 1234= 1+2+3+4 = 10 = 1+0= 1
    the number 1234 could be anyone.
    if the user type 54321, i want that appear in a box the number 6. (5+4+3+2+1=15=1+5=6)

    how i can do it?

    thanks for the help

  2. #2
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi,
    If "yourTextBox" is the instance name of your input text box. Then ....

    Code:
    //some variables to hold you answers.
    var total, finalTotal =0;
    
    //So first loop through your input string add up the numbers and store the 
    //result in the variable "firstTotal".
    
    for(var i = 0;i<yourTextBox.length;i++){
    	firstTotal+=Number(yourTextBox.charAt(i));
    }
    
    //Next turn "firstTotal" into a string
    var newString = String(firstTotal);
    
    //then loop the new string add up the numbers and store the result in the variable "finalTotal".
    
    for(var i = 0;i<newString.length;i++){
    	finalTotal+=Number(newString.charAt(i));
    }
    The final number you want will now be in the variable named finalTotal.
    I am sure there are other ways but this is a pretty easy way.
    Last edited by shipstern; 11-05-2006 at 04:45 PM.

  3. #3
    Peace
    Join Date
    Nov 2006
    Posts
    8

    what a rapid answer

    thank you for your help
    is not working yet because i create a button and i insert the code inside, i create also a box with a finalTotal. when i press the button he reads the yourTextBox and it places the result in finalTotal box, but the finalTotal box says that is NaN. thats not logic because we say that firstTotal is a string and not the finalTotal.

    can we change that?

    thank you

  4. #4
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi again.
    Here is an example that you can use with a button event.

    your text input box has the instance name "yourTextBox"
    your text box to display the final answer has instance name "finalTotal"

    And here is your button event to place in the button.

    Code:
    on(release){
    var first, final = 0;
    for(var i = 0;i<yourTextBox.length;i++){
    	first+=Number(yourTextBox.text.charAt(i));
    }
    var newString = String(first);
    for(var i = 0;i<newString.length;i++){
    	final+=Number(newString.charAt(i));
    }
    finalTotal.text=final;
    }
    This works as I made an test using Flash MX (actionscript version 1).

    Any problems just let me know

  5. #5
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    I felt like extending the idea a bit. This one keeps working until it gets a 1-digit number. For example: 1234567 outputs
    1234567 = 1+2+3+4+5+6+7 = 28 = 2+8 = 10 = 1+0 = 1

    code:
    on(release){
    var curNum:Number;
    var input:String = _parent.input_txt.text;
    var output:String = input;
    do {
    output += " = ";
    curNum = 0;
    for(i = 0; i < input.length-1; ++i) {
    output += input.charAt(i)+"+";
    curNum += Number(input.charAt(i));
    }
    output += input.charAt(input.length-1);
    curNum += Number(input.charAt(input.length-1));
    input = String(curNum);
    output += " = "+curNum;
    } while(curNum >= 10);
    _parent.output_txt.text = output;
    }


  6. #6
    Peace
    Join Date
    Nov 2006
    Posts
    8


    check this out

    on (release) {
    var myString:String = String(inputBox);
    var myCount:Number = 0;
    var myNumber:Number = 0;
    var myCalc:String = "";
    for (i=0; i<myString.length; i++) {
    myNumber += Number(myString.charAt(i));
    myCalc += "+"+myString.charAt(i);
    }
    myCount++;
    trace("PASS #"+myCount+":")
    trace("String = "+myString);
    trace("Calculation = "+myCalc);
    trace("Result = "+myNumber);
    trace("-------------------");
    myString = String(myNumber);
    myNumber = 0;
    myCalc = "";
    for (i=0; i<myString.length; i++) {
    myNumber += Number(myString.charAt(i));
    myCalc += "+"+myString.charAt(i);
    }
    myCount++;
    trace("PASS #"+myCount+":")
    trace("String = "+myString);
    trace("Calculation = "+myCalc);
    trace("Result = "+myNumber);
    trace("-------------------");
    outputBox=myNumber;
    }

    works (a genius guy like you help me out too!)

    Thank you

    i have learned a lot

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center