Copy link to clipboard
Copied
I am counting the number of vowels from a word typed in the program. However am getting a undefined error for the variable totalcount which I identified. I will point out where the error is labeled down below.
// Purpose: To count the number of vowels in a sentence
// This line makes the button, btnCount wait for a mouse click
// When the button is clicked, the countvowels function is called
btnCount.addEventListener(MouseEvent.CLICK, countVowels);
// This is the countVowels function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function countVowels(e:MouseEvent):void
{
//declare the variables
var word:String; // number that will represent each letter
var totalcount:int = 0;
var i:Number;
var numberofvowels:String; //
var stringvalue:String;
// get the sentence from the user
word = txtinWord.text;
// count the vowels in the word
//lblVowels.text = numberofvowels.charAt(i) + "\r";
for(i = 0; i <= numberofvowels.length; i++)
{
stringvalue = word.charAt(i);
//lblVowels.text += stringvalue = "/r"
//word.charAt(i) + " " + String(i) + "\r";
if ((stringvalue == "A") || (stringvalue == "a"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "O") || (stringvalue == "o"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "E") || (stringvalue == "e"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "I") || (stringvalue == "i"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "U") || (stringvalue == "u"))
{
totalcount = totalcount + 1;
}
}
}
lblVowels.text = "There are " + String(totalcount) + " vowels in this sentence."; <----------Over here, I used function String() for total count
here is the program
If you're going to do this guy's homework for him, may as well bust out the regexps.
totalcount = word.match(/[aeiou]/ig).length;
Copy link to clipboard
Copied
that's a lot of code for a pretty simple task:
function vowelCountF(s:String):int{
var vowelA:Array=['a','e','i','o','u'];
var cnt:int=0;
for(var i:int=0;i<vowelA.length;i++){
cnt+=s.toLowerCase().split(vowelA).length-1;
}
return cnt;
}
Copy link to clipboard
Copied
thanks for the reply. Where would I include this code? Should I take something out?
Copy link to clipboard
Copied
remove all your code, add that function and
btnCount.addEventListener(MouseEvent.CLICK, countVowels);
function countVowels(e:MouseEvent):void{
lblVowels.text = "There are " + vowelCountF(txtinWord.text).toString()+ " vowels in this sentence.";
}
Copy link to clipboard
Copied
A little confused, would I add the codes together like this?
function vowelCountF(s:String):int{
var vowelA:Array=['a','e','i','o','u'];
var cnt:int=0;
for(var i:int=0;i<vowelA.length;i++){
cnt+=s.toLowerCase().split(vowelA).length-1;
}
return cnt;
}
btnCount.addEventListener(MouseEvent.CLICK, countVowels);
function countVowels(e:MouseEvent):void{
lblVowels.text = "There are " + vowelCountF(txtinWord.text).toString()+ " vowels in this sentence.";
}
Copy link to clipboard
Copied
No, just change that last line to:
lblVowels.text = "There are " + txtinWord.text.match(/[aeiou]/ig).length + " vowels in this sentence.";
No vowel-counting function needed.
Copy link to clipboard
Copied
I replaced it, and the movie was able to come through but it just gave me "There are 0 vowels" every time I clicked button.
this is my updated code If you are able to see any in corrections that would cause this, your help is really needed.
// Purpose: To count the number of vowels in a sentence
// This line makes the button, btnCount wait for a mouse click
// When the button is clicked, the countvowels function is called
btnCount.addEventListener(MouseEvent.CLICK, countVowels);
// This is the countVowels function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function countVowels(e:MouseEvent):void
{
//declare the variables
var word:String; // number that will represent each letter
var totalcount:int = 0;
var i:Number;
var numberofvowels:String; //
var stringvalue:String;
// get the sentence from the user
word = txtinWord.text;
// count the vowels in the word
//lblVowels.text = numberofvowels.charAt(i) + "\r";
for(i = 0; i <= numberofvowels.length; i++)
{
stringvalue = word.charAt(i);
//lblVowels.text += stringvalue = "/r"
//word.charAt(i) + " " + String(i) + "\r";
if ((stringvalue == "A") || (stringvalue == "a"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "O") || (stringvalue == "o"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "E") || (stringvalue == "e"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "I") || (stringvalue == "i"))
{
totalcount = totalcount + 1;
}
if((stringvalue == "U") || (stringvalue == "u"))
{
totalcount = totalcount + 1;
}
}
}
lblVowels.text = "There are " + txtinWord.text.match(/[aeiou]/ig).length + " vowels in this sentence.";
Copy link to clipboard
Copied
I assume you've verified that txtinWord.text actually contains something.
Copy link to clipboard
Copied
yes it does. You text in any word for vowels to become known. However your code did not work because it doesn't match the rest of the coding I did with the If Statement.
Copy link to clipboard
Copied
I just created a test AS3 movie. I created an input box named txtinWord and put "testing" in it. I created a dynamic text box named lblVowels. I pasted my recommended code into the first frame and ran it.
It displayed "There are 2 vowels in this sentence."
I don't know what you're doing to mess it up.
Copy link to clipboard
Copied
can you paste in the code so I can test it. I assume its pretty much the same code as myn.
Copy link to clipboard
Copied
As I already said, I pasted my recommended code. The code I already posted above, verbatim.
Copy link to clipboard
Copied
I tried that, and it didn't work. If I'm missing something I can just try your whole code so I can test it. You would need the entire code too, to be able to run the movie to caculate vowels.
Copy link to clipboard
Copied
My whole code is nothing more than the code I posted above. Nothing more. Just an input field (with some test text already entered), a dynamic output field, and that one line of code. That's all. There is no more code.
Copy link to clipboard
Copied
That doesn't work on AdobeAnimate2018. You need to declare, give values to your variable and etc. What you gave does not work for me because you're missing everything else. Thank you.
Copy link to clipboard
Copied
pawelk15640242 wrote
That doesn't work on AdobeAnimate2018. You need to declare, give values to your variable and etc.
It does work in Animate CC 2018. I know it works because I did exactly what I said I did and it worked and gave the correct result. If it doesn't work for you then you're messing something up.
Copy link to clipboard
Copied
Whatever, just check out my latest post on function I need to understand.
Copy link to clipboard
Copied
If you're going to do this guy's homework for him, may as well bust out the regexps.
totalcount = word.match(/[aeiou]/ig).length;
Copy link to clipboard
Copied
Hey, why don't you say that to all the other discussions who are asking for help for their own program.
Copy link to clipboard
Copied
hello what does cnt mean?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now