Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function for duplicate numbers and output

    Hello,

    I'm new to javaScript ( a couple of weeks) and I'm just getting used to everything.

    I have an exercise I'm trying to complete.

    What's supposed to happen is when the program is run the user clicks on
    "Select your Numbers!" their then asked for 5 numbers which they input and then the numbers are checked by a function called ”isAlreadySelected” for duplicates and if there is an alert is shown. When 5 numbers are entered correctly then they are shown by a window.alert.

    Its done in to 2 functions

    function isAlreadySelected
    function selectNumbers

    When I run the below code I'm prompted for the 5 numbers but it isn't checking for duplicates and my selection is undefined.



    <HTML>
    <HEAD>
    <TITLE>testing</TITLE>

    <SCRIPT language="JavaScript">



    function isAlreadySelected(newSelection, previousSelections)
    {

    var newSelection = selectNumbers;
    var selectNumbers = 0;
    var previousSelections = new Array;

    for (var index = 0; index < previousSelections.length; index = index +1)

    if (previousSelections[index] == newSelection)

    {

    window.alert('Please make a new selection ',''); // Window alert, to pick another number

    }

    }


    function selectNumbers(highNumber, numberToSelect)
    {



    var highNumber = 0;
    var numberToSelect;
    var numberToSelect = new Array(numberToSelect)

    for (var number = 0; number < numberToSelect.length; number = number + 1)

    {

    numberToSelect[number] = parseFloat(window.prompt('Enter your numbers 1-10 ',''));
    numberToSelect[number] = isAlreadySelected()
    var selection = selectNumbers


    }


    }

    </SCRIPT>
    </HEAD>
    <BODY>

    <STRONG>A test of the functions <BR></STRONG>
    <FORM NAME = "lotteryForm">

    <INPUT TYPE = "button" NAME = "selectBalls" VALUE ="Select your Numbers!"
    ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
    </FORM>

    </BODY>
    </HTML>
    Thanks for any help
    Last edited by Bolean; 05-07-2009 at 04:04 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Your code has quite a few errors, not least the absence of input validation. Try this:-

    Code:
    <script type = "text/javascript">
    
    var selected = new Array();
    var count = 0;
    var newNum;
    var flag = 0;
    
    function selectNumbers() {
    for (var j = 0; j<5; j++) {
    newNum = parseInt(window.prompt('Please make a new selection ','')); // prompt to pick another number
    if (newNum < 0 || newNum >10 || !newNum || isNaN(newNum)) {
    alert ("Invalid number - try again");
    flag = 1;
    j--;
    }  
    
    
    for (var i = 0; i<=selected.length; i++) {
    if (selected[i] == newNum) {
    alert ("That number has already been selected - try again");
    flag = 1;
    j--;
    }  
    }  
    
    if (flag == 0) {
    selected[count] = newNum;
    count++;
    }
    
    flag = 0;
    
    } 
    alert ("The numbers you selected are: " + selected) 
    } 
    
    selectNumbers()
    </script>

    Note that <SCRIPT language="JavaScript"> is deprecated and obsolete. Use <script type = "text/javascript"> instead.

    BTW, please follow the posting guidelines and wrap your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [CODE] and closing [/CODX] - change the X to E - tags . You can edit your previous post.



    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 05-07-2009 at 06:11 PM.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply,

    I'll study your reply as it looks a bit technical for me at the moment, but I guess that the only way I will learn

    Bolean


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •