object error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    object error

    HI all,

    Really struggling to find (what I think) are syntax errors in this code.

    I'm getting a ; expected on line 42 (anArray = new Array[numberToSelect];) and then when i click the button I get an object expected error. I have deleted it and started again but still need your help!

    Regards

    Brendan

    Code:
    <HTML>
    <HEAD>
    <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>
    
    <SCRIPT language="JavaScript">
    
    	
    	/* 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)
    	{
    	
    	  for(var i = 0; i < previousSelectons.length; i = i + 1)
    			{
    				if(newSelection == previousSelction[i])
    					{
    						return true;
    					}
    				else
    					{
    						return false;
    					}
    			}
    	
    	}	
    	
    	
    	
    	
    	function selectNumbers(highNumber, numberToSelect)
    	{
    		
    
    		anArray = new Array[numberToSelect];
    		for(int z = 0; z < numberToSelect; z = z + 1)
    			{
    				anArray[z] = 0;
    				
    		
    			}
    
    	for(var y = 0; y < numberToSelect; y = y + 1)
    
    {
    	var anEntry = parseFloat(window.prompt('please enter your number', ''));
    	
    	while(isAlreadySelected(anEntry, anArray))
    		{
    			anEntry = parseFloat(window.prompt('You have already entered that number - please enter another number'));
    		}
    		
    		
    			anArray[y] = anEntry;
    		
    	
    }			
    		
    	return anArray
    	
    	}
    		
    	
    	
    </SCRIPT>
    </HEAD>
    <BODY>
    
    	<STRONG>A test of the function selectNumbers()<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>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by brendanmcdonagh
    I'm getting a ; expected on line 42 (anArray = new Array[numberToSelect];)
    well, objects are created using Functions, the correct call would be
    Code:
    anArray = new Array();
    // or
    anArray = new Array;
    // or (simplified)
    anArray = [];
    see MDC – Array for a list of constructor options.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Also, check your spellings in isAlreadySelect ed, e.g. lines 21 and 23.

      Comment

      • DaiOz
        New Member
        • May 2009
        • 16

        #4
        Hi guys,
        Ref the above code as your probably aware im also doing the same course, i've got the above code to work but I find that it will only prompt for a new number if duplicated for the first number and won't check the next 4.
        Any suggestions?

        Code:
        	function isAlreadySelected(newSelection, previousSelections)
        	{
        
          
               for(var i = 0; i < previousSelections.length; i = i + 1)
                     {
                         if(newSelection == previousSelections[i])
                             {
                                 return true;
                             }
                         else
                             {
                                 return false;
                             }
        	
        	
        			}	
        	
        	}
        Last edited by acoder; May 10 '09, 03:47 PM. Reason: Added [code] tags + removed full code

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Follow the code that you've posted. The else part is causing the problem. It just tries to match the first element of the array and if not, returns false. You need to match against all the elements/items in the array, not just the first one.

          PS. Since this is coursework/homework, I've removed the irrelevant code as per the posting guidelines.

          PPS. Please use [code] tags when posting code.

          Comment

          • DaiOz
            New Member
            • May 2009
            • 16

            #6
            Any hints how this can be done? Ive even resorted to the libary to help lol

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              As I said, follow what the code does. Perhaps add a few alerts or use a debugger to follow the values of the variables in the loop.

              A hint: when you enter the loop, it compares newSelection with the first item in previousSelecti ons. If it matches, it returns true. If it doesn't match, it returns false. That's it! It doesn't even try to compare with the rest of the items in the array. So, you need to remove the else, but you still need to return false if it doesn't match any of the items in the array.

              Comment

              • DaiOz
                New Member
                • May 2009
                • 16

                #8
                Originally posted by acoder
                use a debugger to follow the values of the variables in the loop.
                Im a complete noob at this and was wondering what you meant by a debugger?
                Sorry if im testing your patientence im just trying to get my head around this and find it easier learning from working code but can appreciate your trying to make me learn and discover the answer but I think we could be here some time lol

                Comment

                • DaiOz
                  New Member
                  • May 2009
                  • 16

                  #9
                  I think i've nearly done it, solved the else problem but it will still only check the first 2 inputed numbers, help please :)

                  Code:
                  function isAlreadySelected(newSelection, previousSelections)
                  	{
                  
                    
                         for(var i = 0; i < previousSelections.length; i = i++)
                               {
                                   if(newSelection == previousSelections[i])
                                       {
                                           return true;
                                       }
                                   else
                  				 
                  				 if(newSelection != previousSelections[i])
                  					{
                  					  return false;
                                       }
                                  
                  	
                  	
                  			}	
                  	
                  	}

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That doesn't really solve the problem. Look at this code:
                    Code:
                    for (i = 0; i < anArray.length; i++) {
                        if (something == anArray[i]) return true;
                    }
                    what this does is loops over the array and if 'something' matches any of the items in the array (even if it's the last one), then it returns true. All that's left is to return false when it doesn't match any items, so you need to figure out where in the code you know that none of the items have been matched.

                    Comment

                    • DaiOz
                      New Member
                      • May 2009
                      • 16

                      #11
                      I finally cracked this and realized I just needed to remove the else command which you did blatantly say lol
                      Thanks for the help again it may take me a while but I get there in the end lol

                      Comment

                      • Oli2uok
                        New Member
                        • May 2009
                        • 8

                        #12
                        Task 2

                        Hi Im also doing th M150 course and really struggling to get the Task 2 to work..any advice anyone..help..


                        Code:
                        <HTML>
                        <HEAD>
                        <TITLE>M150 TMA 5 : Programming : Task 2 - Drawing numbered balls</TITLE>
                        
                        <SCRIPT language="JavaScript">
                        	
                         //and display it in an alert window  
                        
                        	function getRandomNumber(aNumber) 
                        {   
                        		var getRandomNumber = Math.floor(Math.random()*49);
                        
                        
                        	function lotteryDraw(highBall, numberToDraw)
                        	{
                        		
                        		var numberPool = new Array(highBall);
                        	
                        		var sizeOfPool = highBall;
                        
                        		var drawnNumbers = new Array(numberToDraw);
                        	 
                        		for (var index = 0; index < highBall; index = index + 1)
                        		{
                        			numberPool[index] = index + 1; 
                        		}
                        	
                        		for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1)
                        		{
                        			var randomNumber = getRandomNumber(sizeOfPool);
                        	
                        			drawnNumbers[drawnBall] = numberPool[getRandomNumber];
                        		
                        			numberPool[getRandomNumber] = numberPool[sizeOfPool - 1];
                        				
                        			sizeOfPool = sizeOfPool - 1;
                        		}
                        			
                        		lotteryForm.drawnNumbersTextBox.value = drawnNumbers;
                        	}
                        
                        	}
                        
                        </SCRIPT>
                        </HEAD>
                        <BODY>
                        
                        	<STRONG>A test of the function lotteryDraw()<BR></STRONG>
                        	<FORM NAME = "lotteryForm">
                        	<P>
                        	Total Number of Balls in Pool:
                        	</P>
                        	<P>
                        	<INPUT TYPE = "text" NAME = "numberOfBallsTextBox"  SIZE ="2" MAXLENGTH = "2">
                        	</P>
                        	<P>
                        	Number of Balls to Draw:
                        	</P>
                        	<P>
                        	<!-- Note the SIZE attribute, whose value determines the size in characters of a text box and
                        	the MAXLENGTH attribute, whose value constrains the length in characters of the allowed input -->
                        	<INPUT TYPE = "text" NAME = "ballsToDrawTextBox" SIZE = "1" MAXLENGTH = "1" >
                        	</P>
                        		
                        	
                        	<!-- The RESET input type creates a button which, when clicked, resets a form to its original state -->
                        	<INPUT TYPE = "reset" NAME = "resetButton" VALUE = "Clear Form">
                        	
                        	<!-- COMPLETE THE ONCLICK EVENT HANDLER TO INVOKE lotteryDraw() WITH ARGUMENTS TAKEN FROM THE FORM INPUT TEXT BOXES -->
                        	<INPUT TYPE = "button" NAME = "drawBalls"  VALUE ="Draw the Balls!"
                        						ONCLICK = "lotteryDraw()" = "drawNumbersTextBox">
                        	
                        	<P>
                        	Drawn Numbers:
                        	</P>
                        	<P>
                        	<INPUT TYPE = "text" NAME = "drawnNumbersTextBox" SIZE = "30" MAXLENGTH = "30" >
                        	</P>
                        	
                        	</FORM>
                        
                        </BODY>
                        </HTML>
                        Cheers
                        Last edited by Dormilich; May 13 '09, 06:16 PM. Reason: Please use [code] tags when posting code

                        Comment

                        • DaiOz
                          New Member
                          • May 2009
                          • 16

                          #13
                          Hi mate,
                          I know exactly how ya feel lol
                          Take a closer look at (aNumber) and then look at the function for lottery draw.
                          I'd give you the answer but I cant risk all my hard work so far its been stressfull lol

                          Comment

                          • Oli2uok
                            New Member
                            • May 2009
                            • 8

                            #14
                            more help..lol

                            Originally posted by DaiOz
                            Hi mate,
                            I know exactly how ya feel lol
                            Take a closer look at (aNumber) and then look at the function for lottery draw.
                            I'd give you the answer but I cant risk all my hard work so far its been stressfull lol
                            I sort of know what ya mean but dont..i know im having a hard time at the mo.lol
                            any other advice would help!!

                            Comment

                            • DaiOz
                              New Member
                              • May 2009
                              • 16

                              #15
                              Maybe take a look at the code below in particular,
                              Code:
                               for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1)
                                      {
                                           var randomNumber = getRandomNumber(sizeOfPool);
                                
                                           drawnNumbers[drawnBall] = numberPool[getRandomNumber];
                                
                                           numberPool[getRandomNumber] = numberPool[sizeOfPool - 1];
                                
                                           sizeOfPool = sizeOfPool - 1;

                              Comment

                              Working...