-
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
-
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.
-
-
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
-
Bacon-wrapped closures
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;
}
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|