Skip to main content
pawelk15640242
Known Participant
July 24, 2018
Answered

I am getting a undefined error! ActionScript3.0

  • July 24, 2018
  • 1 reply
  • 1605 views

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

This topic has been closed for replies.
Correct answer ClayUUID

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;

1 reply

kglad
Community Expert
Community Expert
July 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;

}

pawelk15640242
Known Participant
July 24, 2018

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

kglad
Community Expert
Community Expert
July 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.";         

}