Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

I am getting a undefined error! ActionScript3.0

Community Beginner ,
Jul 24, 2018 Jul 24, 2018

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

1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 24, 2018 Jul 24, 2018

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;

Translate
Community Expert ,
Jul 24, 2018 Jul 24, 2018

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 24, 2018 Jul 24, 2018

thanks for the reply. Where would I include this code? Should I take something out?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 24, 2018 Jul 24, 2018

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.";         

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 24, 2018 Jul 24, 2018

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.";        

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 24, 2018 Jul 24, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 24, 2018 Jul 24, 2018

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.";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 24, 2018 Jul 24, 2018

I assume you've verified that txtinWord.text actually contains something.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 24, 2018 Jul 24, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 25, 2018 Jul 25, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2018 Jul 25, 2018

can you paste in the code so I can test it. I assume its pretty much the same code as myn.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 25, 2018 Jul 25, 2018

As I already said, I pasted my recommended code. The code I already posted above, verbatim.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2018 Jul 25, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 25, 2018 Jul 25, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2018 Jul 25, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 25, 2018 Jul 25, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2018 Jul 25, 2018
LATEST

Whatever, just check out my latest post on function I need to understand.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 24, 2018 Jul 24, 2018

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 24, 2018 Jul 24, 2018

Hey, why don't you say that to all the other discussions who are asking for help for their own program.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2018 Jul 25, 2018

hello what does cnt mean?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines