Results 1 to 4 of 4

Thread: JavaScript function help

  1. #1
    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>

  2. #2
    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.

  3. #3
    Join Date
    May 2009
    Posts
    4

    Re: JavaScript function help

    The .equals() function does not work with javascript, it works with java. Thanks anyway.

  4. #4
    Join Date
    Feb 2009
    Posts
    96

    Re: JavaScript function help

    Yeah i miss read the title thought it said JAVA

Similar Threads

  1. Passing XSL value to javascript function
    By Steadfast in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 11:51 PM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. Eval function in javascript
    By Solaris in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 05:59 PM
  4. Javascript isNull function
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 11-11-2009, 02:51 PM
  5. JavaScript Sleep Function
    By DARIELLE in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 09:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,883,365.30630 seconds with 17 queries