Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-05-2009
Member
 
Join Date: May 2009
Posts: 4
question JavaScript function help

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>
Reply With Quote
  #2  
Old 24-05-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: JavaScript function help

yoiu should use the .equals() instead of == . The reason is that == is best used for numbers while .equals makes a direct comparison.
Reply With Quote
  #3  
Old 26-05-2009
Member
 
Join Date: May 2009
Posts: 4
Re: JavaScript function help

The .equals() function does not work with javascript, it works with java. Thanks anyway.
Reply With Quote
  #4  
Old 26-05-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: JavaScript function help

Yeah i miss read the title thought it said JAVA
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "JavaScript function help"
Thread Thread Starter Forum Replies Last Post
Passing XSL value to javascript function Steadfast Software Development 6 13-05-2010 11:51 PM
c# function equivalent to gettime function in javascript Omaar Software Development 4 10-03-2010 10:44 PM
Eval function in javascript Solaris Software Development 5 02-03-2010 05:59 PM
Javascript isNull function Chrisch Software Development 3 11-11-2009 02:51 PM
JavaScript Sleep Function DARIELLE Software Development 3 29-06-2009 09:30 AM


All times are GMT +5.5. The time now is 03:39 AM.