View Full Version : Can anyone help with this ActionScript
Cyan Star
02-21-2003, 02:56 AM
Greetings,
I'm not sure this counts as Simple Stuff... But I certainly am quite a newbie to ActionScript, apart from fairly basic navigation stuff.
I'm using Flash MX and I've spent a couple of days reading the manual & searching for tutorials etc. but I'm still clueless.
What I want to do is: Convert the individual characters of a word, entered by a user into an Input Text field, into a series of single digit numbers (one for each letter of the word) and have them displayed and then added together until they resolve to a single digit number. eg:
45346724 = 35 = 8
TESTWORD
Obviously, if the word produced a number greater than 9, an extra field would be required to resolve it to a single digit.
The alphabet is numbered as follows:
a, i, j, q, y = 1
b, k, r = 2
c, g, l, s = 3
d, m, t = 4
e, h, n, x = 5
u, v, w = 6
o, z = 7
f, p = 8
Any help with this would be much appreciated, or even some pointers to articles that may be of use.
Regards to all,
Cyan.
P.S. Moderator, If this is in the wrong section, please feel free to move it to the most appropriate one.
Pseudo code
mystring.tolowercase
create array equal to string lendth
for 1 to string length
use myString.charCodeAt(index) to put char code in equivelant position in array
alter char code to become number your want
next
while array length>1
add numbers together in shorter temp array
swap temp array with main array
end while
print to screen
That should get you started.
fgf
avatar
02-21-2003, 11:57 AM
Cyan Star,
Never did anything like it, so I thought it would be a good challange... :)
Maybe fgf and you could have a look at the code.
Corrections, better (simple) coding suggestions...
I didn't test it properly (leave that up to you)
Cyan Star
02-22-2003, 05:00 AM
Greetings fgf and avatar.
Blessings to you both and much gratitude for your inputs.
avatar, your attachment far exceeded my expectations, much respect for your enthusiasm.
Unfortunately, my present knowledge of ActionScript is insufficient for me to determin whether your code could be improved but... I'm all in favour of increased efficiency so, any contributions from anyone would be graciously welcomed.
From the brief testing I have done, it seems to be working fine. :)
I would very much like to understand the principle of its operation so as to be able to develop it further but i've just returned from a late night out and need some sleep. I will spend plenty of time studying it when I awake, you can be sure of that.
The first thing I want to do with it is, set it up so that all the calculations that go to make the final result are also displayed on the stage.
Also, instead of converting the string to lower case, I would prefer to preserve the original string (as typed by the user) and display a copy of it in capitals, spaced in such a way as to allow the individual letter numbers to be displayed above the word in alignment. So it looks similar to this:
User Input = Michael
Result = 4 1 3 5 1 5 3 = 22 = 4
Result = M I C H A E L
This will also give me the opportunity to ask the user to confirm that the word is spelled correctly.
Obviously, I don't expect all this to be done for me but, as I wish to develop this much further, it is why I want to fully understand all the parameters that make it work.
Kind regards,
Cyan.
avatar
02-24-2003, 08:35 AM
There might be more champagne than blood in my veins right now, but I'll give it a try...
onClipEvent (enterFrame) {
if (_root.makeCode == true) {
// if you want this can be changed to UpperCase();
_root.input = _root.input.toLowerCase();
input = new Array();
// converts characters to numbers and results in a sum at the end
for (i=0; i<_root.input.length; i++) {
input[i] = _root.input.charAt(i);
trace(input[i]);
if (input[i] == "a" || input[i] == "i" || input[i] == "j" || input[i] == "q" || input[i] == "y") {
input[i] = 1;
...
trace(input[i]);
sum += input[i];
trace("sum = "+sum);
}
// if this sum is bigger than 9 make a sum of the numbers (16 = 1+ 6) and repeat this until the result is smaller than 9 (88 = 8 + 8 = 16 = 6 + 1)
while (sum>9) {
sumString = String(sum);
trace("sum.length ="+sumString.length);
sumAr = new Array();
for (i=0; i<sumString.length; i++) {
sumAr[i] = parseInt(sumString.charAt(i));
trace("sumAr[i] = "+sumAr[i]);
sumFinal += sumAr[i];
}
sum = sumFinal;
sumFinal = undefined
}
//display the result
trace("---");
trace("finalsum = "+sum);
_root.result = sum;
_root.makeCode = false;
}
//reset the variables
if (_root.makeCode == false) {
sum = undefined;
i = undefined;
}
}
If you still have some questions please let me know...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.