Not sure how to reference the actual arguments of the isAlreadySelected() fuction in the body of selectNum() function properly. The description of the fuctions are enclosed in /* */ and coloured green, this is what the fuctions are supposed to do. Having trouble populating an array and returning it in the selectNum() function . Need to use all varables in the function. Any help would be much appreciated **isAlredySelected() fuction is ok.
Here goes:
/* Determines if a given ball has already been selected.
The function takes two arguments:
a whole number which is a new selection
an array which contains previous selections.
The function returns:
true if the new selection exists in the previous selections
false otherwise
*/ function isAlreadySelected(newSelection, previousSelections)
{
var prevArray = new Array(previousSelections);
parseFloat(newSelection);
if (newSelection == previousSelections)
{
document.write('already selected.', '');
return true;
}
else
{
document.write('not been selected.', '');
return false;
return newSelection;
}
}
/* The function prompts users to select numbers from a pool of balls
The function takes two arguments:
the highest number in the pool of balls
the number of balls to be selected
The user is prompted to make choices.
A check is made that a choice is not a duplicate.
The user is reprompted to make a choice if a selection has been selected already
The function returns an array of numbers selected by the user
*/ function selectNum(highNumber, numberToSelect)
{
// Alert user to the valid range of balls to be selected window.alert('Enter numbers between 1 and ' + highNumber + '.', '');
parseFloat(numberToSelect);
var numArray = new Array(numberToSelect);
var current = 0; //not sure about this
var selectArray = new Array(current); //not sure about this
for (var index = 1; index < numberToSelect; index = index + 1)
{
numSelArray[index] = parseFloat(window.prompt('Please enter your ball selection.', ''));
while (isAlreadySelected(current, numSelArray[index]) == true)//got this incorrect
{
window.alert('Current ball has already been selected.', '');
numSelArray[index] = parseFloat(window.prompt('Please enter your ball selection.', ''));
}
selectArray[current] = numSelArray[index];
}
return selectArray(current);//and this is not right
}
</SCRIPT>
</HEAD>
<BODY>//the rest of this is ok, but provided to show what to expect
<STRONG>A test of the function selectNumbers()<BR></STRONG>
<FORM NAME = "lotteryForm">
<INPUT TYPE = "button" NAME = "selectBalls" VALUE ="Select your Numbers!"
ONCLICK = "var sel = selectNum(20, 8);
window.prompt('You selected: ' + sel);"></P>
Bookmarks