Advertisement
BOT_Yokel

ICS3U1 Unit 3 Activity 2: Question 1

Jul 25th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date: July 25., 2017
  2. // Purpose: To store, track, display the scores, and calculate statistics for an 18 round golf game
  3.  
  4. // These lines make the TextInput wait for a mouse click
  5. // When any of these components are clicked, the clearInputfunction is called
  6. txtinScore.addEventListener(MouseEvent.CLICK, clearInput);
  7.  
  8. // This line makes the button, btnAddScore wait for a mouse click
  9. // When the button is clicked, the addScore function is called
  10. btnAddScore.addEventListener(MouseEvent.CLICK, addScore);
  11.  
  12. // This line makes the button, btnDisplayScore wait for a mouse click
  13. // When the button is clicked, the displayNames function is called
  14. btnDisplayScore.addEventListener(MouseEvent.CLICK, displayNames);
  15.  
  16. // declare the global variables
  17. var ScoreCount:Array = new Array(); // array of friends' names
  18. lblPrompt1.text = "Enter the score for hole #" + (ScoreCount.length + 1)
  19.  
  20. // This is the addName function
  21. // e:MouseEvent is the click event experienced by the button
  22. // void indicates that the function does not return a value
  23. function addScore(e:MouseEvent):void
  24. {
  25.     // declare the variables
  26.     var Score:String; // Score Count entered by User
  27.     // get the score from the user
  28.     Score = txtinScore.text;
  29.     // append the score to the end of the array
  30.     ScoreCount.push(Score);
  31.     // Update the current hole number in the prompt
  32.     if (ScoreCount.length <=17)
  33.     {
  34.         lblPrompt1.text = "Enter the score for hole #" + (ScoreCount.length + 1);
  35.     }
  36.     // Disable score adding if maximum hole count is reached
  37.     else
  38.     {
  39.         lblPrompt1.text = "All scores are entered.";
  40.         txtinScore.text = "";
  41.         btnAddScore.enabled = false;
  42.     }
  43.     // display the length of the array in the label
  44.     lblArraySize.text = "Number of Golf Scores Entered: " + ScoreCount.length;
  45. }
  46.  
  47. // This is the displayNames function
  48. // e:MouseEvent is the click event experienced by the button
  49. // void indicates that the function does not return a value
  50. function displayNames(e:MouseEvent):void
  51. {
  52.    
  53.     lblTotal.text = "";
  54.     for (var x=0; x < ScoreCount.length; x++)
  55.     {
  56.         lblTotal.text += "Hole " + (x+1) + ": " + ScoreCount[x] + "\r";
  57.     }
  58.     lblStats.text = "Total: " + totalValue(ScoreCount) + "\r" + "Lowest Score: " + minValue(ScoreCount) + "\r" + "Highest Score: " + maxValue(ScoreCount);
  59. }
  60.  
  61. // This is function totalValue
  62. // ScoreCount array of numbers
  63. // returns the average of all the values in the array
  64. function totalValue(ScoreCount):Number
  65. {
  66.     var total:Number = 0;
  67.     for (var x:int=0; x < ScoreCount.length; x++)
  68.     {
  69.         total += int(ScoreCount[x]);
  70.     }
  71.     return total;
  72. }
  73.  
  74. // This is function maxValue
  75. // ScoreCount array of numbers
  76. // returns the highest value in the array
  77. function maxValue(ScoreCount):Number
  78. {
  79.     var highest:Number = ScoreCount[0];
  80.     for (var x:int=1; x<ScoreCount.length; x++)
  81.     {
  82.         if (ScoreCount[x] > highest)
  83.         {
  84.             highest = ScoreCount[x];
  85.         }
  86.     }
  87.     return highest;
  88. }
  89.  
  90. // This is function minValue
  91. // ScoreCount array of numbers
  92. // returns the highest value in the array
  93. function minValue(ScoreCount):Number
  94. {
  95.     var lowest:Number = ScoreCount[0];
  96.     for (var x:int=1; x<ScoreCount.length; x++)
  97.     {
  98.         if (ScoreCount[x] < lowest)
  99.         {
  100.             lowest = ScoreCount[x];
  101.         }
  102.     }
  103.     return lowest;
  104. }
  105.  
  106. // This is the clearInput function
  107. // e:MouseEvent is the click event experienced by the textInput
  108. // void indicates that the function does not return a value
  109. function clearInput(e:MouseEvent):void
  110. {
  111.      txtinScore.text = "";
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement