Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date: July 25., 2017
- // Purpose: To store, track, display the scores, and calculate statistics for an 18 round golf game
- // These lines make the TextInput wait for a mouse click
- // When any of these components are clicked, the clearInputfunction is called
- txtinScore.addEventListener(MouseEvent.CLICK, clearInput);
- // This line makes the button, btnAddScore wait for a mouse click
- // When the button is clicked, the addScore function is called
- btnAddScore.addEventListener(MouseEvent.CLICK, addScore);
- // This line makes the button, btnDisplayScore wait for a mouse click
- // When the button is clicked, the displayNames function is called
- btnDisplayScore.addEventListener(MouseEvent.CLICK, displayNames);
- // declare the global variables
- var ScoreCount:Array = new Array(); // array of friends' names
- lblPrompt1.text = "Enter the score for hole #" + (ScoreCount.length + 1)
- // This is the addName function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function addScore(e:MouseEvent):void
- {
- // declare the variables
- var Score:String; // Score Count entered by User
- // get the score from the user
- Score = txtinScore.text;
- // append the score to the end of the array
- ScoreCount.push(Score);
- // Update the current hole number in the prompt
- if (ScoreCount.length <=17)
- {
- lblPrompt1.text = "Enter the score for hole #" + (ScoreCount.length + 1);
- }
- // Disable score adding if maximum hole count is reached
- else
- {
- lblPrompt1.text = "All scores are entered.";
- txtinScore.text = "";
- btnAddScore.enabled = false;
- }
- // display the length of the array in the label
- lblArraySize.text = "Number of Golf Scores Entered: " + ScoreCount.length;
- }
- // This is the displayNames function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function displayNames(e:MouseEvent):void
- {
- lblTotal.text = "";
- for (var x=0; x < ScoreCount.length; x++)
- {
- lblTotal.text += "Hole " + (x+1) + ": " + ScoreCount[x] + "\r";
- }
- lblStats.text = "Total: " + totalValue(ScoreCount) + "\r" + "Lowest Score: " + minValue(ScoreCount) + "\r" + "Highest Score: " + maxValue(ScoreCount);
- }
- // This is function totalValue
- // ScoreCount array of numbers
- // returns the average of all the values in the array
- function totalValue(ScoreCount):Number
- {
- var total:Number = 0;
- for (var x:int=0; x < ScoreCount.length; x++)
- {
- total += int(ScoreCount[x]);
- }
- return total;
- }
- // This is function maxValue
- // ScoreCount array of numbers
- // returns the highest value in the array
- function maxValue(ScoreCount):Number
- {
- var highest:Number = ScoreCount[0];
- for (var x:int=1; x<ScoreCount.length; x++)
- {
- if (ScoreCount[x] > highest)
- {
- highest = ScoreCount[x];
- }
- }
- return highest;
- }
- // This is function minValue
- // ScoreCount array of numbers
- // returns the highest value in the array
- function minValue(ScoreCount):Number
- {
- var lowest:Number = ScoreCount[0];
- for (var x:int=1; x<ScoreCount.length; x++)
- {
- if (ScoreCount[x] < lowest)
- {
- lowest = ScoreCount[x];
- }
- }
- return lowest;
- }
- // This is the clearInput function
- // e:MouseEvent is the click event experienced by the textInput
- // void indicates that the function does not return a value
- function clearInput(e:MouseEvent):void
- {
- txtinScore.text = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement