I have a string returning in an array. I'm sending the contents of that string
to a function to test if the string contains a letter. If the string only
contains one letter it works fine. I can't get it to work if it contains more
than one. Please help...
function convertConstruction (tempCon):String
{
var convertCon:Array = new Array();
if (tempCon == "A")
{
convertCon.push("Alum");
}
if (tempCon == "B")
{
convertCon.push("Brick");
}
return convertCon.toString();
}
what am i missing?
C-Rock,
> I have a string returning in an array. I'm sending the contents
> of that string to a function to test if the string contains a letter.
A string can't return an array. What do you mean? An array's contents
might be any number of datatypes -- an array of strings, numbers, movie clip
references, a combination of things -- but a string is a string.
Based on what you're looking for -- does this string contain a
particular letter? -- let's see what you have.
> If the string only contains one letter it works fine. I can't get it to
> work if it contains more than one. Please help...
function convertConstruction (tempCon):String {
var convertCon:Array = new Array();
if (tempCon == "A") {
convertCon.push("Alum");
}
... so far, so good -- more or less. Your function receives a parameter,
tempCon. Since you're typing your function, you may as well type this
parameter.
function convertConstruction (tempCon:String):String {
... and unless this function is returning a string, it will need something
besides that second :String. Skipping ahead, I see you're returning
convert.Cont.toString(), which, yes, is a String. So you're good. Let's
move on.
if (tempCon == "B") {
convertCon.push("Brick");
}
return convertCon.toString();
}
So, let's say the parameter passed in was "A". That first if()
statement would catch it. Let's say the parameter was "B". Still good, the
second if() would catch it.
I should mention, I'm confused why you're using an array at all. I know
you want to check for letters if the incoming string has more than one
character, but you don't need an array for that. Let's keep going, though.
Let's say the incoming parameter was "Cat". If first if() will check to
see if "Cat" is "A" ... which it isn't. The second if() will check if "Cat"
is "B" ... which it isn't. So, finally, the return statement will kick back
an empty array, convertCon, that has been converted to a string. You'll get
an empty string as your return value.
Here's what you want. You want to count individual characters in the
incoming string. You *could* break up each letter and put each letter into
an array, but that's unnecessary. Check out the String class entry of the
ActionScript Language Reference: you'll see that the String class has every
thing you need. It has a length property that counts the characters and a
charAt() method that get's a character from a given location. Wow! Good
stuff.
So ...
function convertConstruction(tempCon:String):String {
for (var n = 0; n < tempCon.length; n++) {
if (tempCon.charAt(n) == "A") {
trace("found an A");
}
}
return tempCon;
}
We're using a for() loop to count through the number of characters in
the incoming string. Then we're checking each letter to see if it's an A.
Let's make this tighter. The incoming string might be a lowercase "a,"
right? So let's always convert the incoming string to uppercase before
checking.
// snip
if (tempCon.toUpperCase().charAt(n) == "A") {
// snip
This way, it doesn't matter what case is passed in. The uppercase
version will always be checked against that capital "A". Now, in real life,
you don't just want to trace "found an A"; it seems you want to return an
A-word for that letter, a B-word for "B," and so on.
You could do this with a series of if() statements ...
function convertConstruction(tempCon:String):String {
for (var n = 0; n < tempCon.length; n++) {
if (tempCon.toUpperCase().charAt(n) == "A") {
return "Alum";
}
if (tempCon.toUpperCase().charAt(n) == "B") {
return "Brick";
}
}
}
... or you can use a switch() statment, which, to my thinking, is easier to
read in this context.
function convertConstruction(tempCon:String):String {
for (var n = 0; n < tempCon.length; n++) {
switch (tempCon.toUpperCase().charAt(n)) {
case "A" :
return "Alum";
case "B" :
return "Brick";
}
}
}
trace(convertConstruction("cat"));
Note: look carefully at the logic of this function. The way it's set
up, a word like "cat" will return "Alum" if there's an "A" anywhere in the
word. What return value would you get for a word like "bat"? Would you get
"Alum," or "Brick"? See if you can figure it out ... and why. Just think
carefully through the answer, and see if this is what you need your function
to do.
David
stiller (at) quip (dot) net
"Luck is the residue of good design."