Reputation:
I am making a program which has the user enter a golf score which then stores it in an array. However, the user can only enter up to 18 scores and I have attempted to code a prompt which indicates the number of scores that are in the array/they have entered. The label is called lblPrompt1 and it does not function. I also want to disable the addscore button when the user has entered all 18 scores. The prompt does not function. Please advise. Thanks!
// Purpose: To add golf scores to an array
// This line makes the button, btnAddScore wait for a mouse click
// When the button is clicked, the addName function is called
btnAddScore.addEventListener(MouseEvent.CLICK, addScore);
// This line makes the button, btnDisplayScores wait for a mouse click
// When the button is clicked, the displayScores function is called
btnDisplayScores.addEventListener(MouseEvent.CLICK, displayScores);
// declare the global variables
var scores: Array = new Array(); // array of golf scores
// 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 golfScore: String; // friend's name entered by user
// get the name from the user
golfScore = txtinScore.text;
// append the name to the end of the array
scores.push(golfScore);
// display the length of the array in the label
lblArraySize.text = "Number of Golf Scores Entered: " + scores.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 displayScores(e: MouseEvent): void {
var holeNumber: Number;
lblScores.text = "";
for (var x = 0; x < scores.length; x++) {
lblScores.text += scores[x] + "\r";
}
holeNumber++;
if (holeNumber <= 18) {
lblPrompt1.text = "Enter the score for hole #" + holeNumber.toString() + ":";
} else {
lblPrompt1.text = "All scores are entered.";
txtinScore.text = "";
btnAddScore.enabled = false;
}
}
Upvotes: 1
Views: 82
Reputation: 14406
While it's not very clear what you are asking, one issue you have is that your holeNumber
variable will never have a numeric value - it will always be NaN
(Not A Number).
Whenever the display scores button is clicked, inside the click handler function (displayScores
), you create this variable (holeNumber
) and you don't give it a value. Numbers default to NaN
, so later when you increment it with holeNumber++
, you'll just end up with NaN
still (because NaN
plus 1
is still NaN
).
The other part of that issue, is you create the variable in the scope of the click handler, and only increment it once, so even if you changed the var definition to var holeNumber:Number = 0;
, it would still have a value of 1
every time you clicked because every click the variable get's recreated and then incremented by 1.
What you probably want to do, is forgo the holeNumber
var altogether and just reference scores.length
as that is essentially the current hole.
function displayScores(e: MouseEvent): void {
lblScores.text = "";
for (var x = 0; x < scores.length; x++) {
lblScores.text += scores[x] + "\r";
}
//use scores.length instead of holeNumber
if (scores.length < 18) {
lblPrompt1.text = "Enter the score for hole #" + String(scores.length + 1) + ":";
//adding 1 to the length because that would be the next hole number
} else {
lblPrompt1.text = "All scores are entered.";
txtinScore.text = "";
btnAddScore.enabled = false;
}
}
Upvotes: 3