object error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Oli2uok
    New Member
    • May 2009
    • 8

    #16
    Originally posted by DaiOz
    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;
    I think I have lost the plot!! everything seems goggly to me..lol.

    Comment

    • Oli2uok
      New Member
      • May 2009
      • 8

      #17
      Originally posted by Oli2uok
      I think I have lost the plot!! everything seems goggly to me..lol.
      Heeeeelllpppp!! !! is it just me!!!!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Originally posted by Oli2uok
        Hi Im also doing th M150 course and really struggling to get the Task 2 to work..any advice anyone..help..
        Since this is a course, we can't give you the answer, but we can help you solve it. The first thing to do is describe what your code should be able to do, what it currently does and any errors you see in the error console.

        Comment

        • Oli2uok
          New Member
          • May 2009
          • 8

          #19
          Andy help?

          Code:
          	<!-- 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 = "document.lotteryForm.drawnNumbersTextBox.value = lotteryDraw().value">
          If anyone can point me in the right direction it will help..all im getting is undefind!! point me to where the problem is and it will be a good help..regards

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            lotteryDraw() expects two arguments, but you haven't given it any. Pass the form values. Also, the .value after the function call would be incorrect unless it returns an element.

            Comment

            • Oli2uok
              New Member
              • May 2009
              • 8

              #21
              wrong output!! ??

              Code:
              function getRandomNumber(aNumber) 
              {   
              var getRandomNumber = Math.floor(Math.random());
              
              	return aNumber;
              }
              
              	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) 
                      {     
                          //creates an array of all the numbers that can be possibly drawn !
              			
                          numberPool[index] = index + 1;  
                      } 
                
                
                      for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1) 
                      {
                
                          var randomNumber = getRandomNumber(sizeOfPool); 
                
                          drawnNumbers[drawnBall] = numberPool[randomNumber]; 
                
                          numberPool[randomNumber] = 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 = " var first = document.lotteryForm.ballsToDrawTextBox.value; var second = document.lotteryForm.numberOfBallsTextBox.value; lotteryDraw(first, second)">
              	
              	<P>
              	Drawn Numbers:
              	</P>
              	<P>
              	<INPUT TYPE = "text" NAME = "drawnNumbersTextBox" SIZE = "30" MAXLENGTH = "30" >
              	</P>
              	
              	</FORM>
              
              </BODY>
              </HTML>
              Hi I think I may nearly have it. when it is run the output is 6.5.4.3.2.1 this is from number of balls drawn form 49 input. it should give me 6 random numbers between 1 and 49 not 6 down to 1!! any ideas where im going wrong, or any possible changes to my code needed. Cheers for any help given..nearing the end of this task now..lol

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                I think you've got the two paramters to lotteryDraw mixed up. Follow the code and the values input.

                Comment

                • Oli2uok
                  New Member
                  • May 2009
                  • 8

                  #23
                  Thanks

                  Hi,
                  Thanks for the reply I had realised what I had done about an hour after I had asked for help.

                  Cheers

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #24
                    OK, no problem. I didn't get a chance to look at it until now. Anyway, good to see that you managed to figure it out yourself.

                    Comment

                    • Oli2uok
                      New Member
                      • May 2009
                      • 8

                      #25
                      Return of true!

                      You may be able to help me again though lol. Im stuck on this one. I keep getting a return of true after the 1st input!!
                      Any ideas!!

                      Cheers
                      Code:
                        
                          function isAlreadySelected(newSelection, previousSelections) 
                          { 
                         
                          for(var index = 0; index < previousSelections.length; index = index + 1)  
                             {  
                                  if(newSelection == previousSelections[index])  
                                  {  
                                  
                                      return true;  
                                  }  
                                  else  
                                  {  
                                
                                      return false;  
                                  }  
                              }  
                        
                          }
                      Last edited by acoder; May 19 '09, 11:21 AM. Reason: Removed irrelevant code

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #26
                        See the previous posts from #4 - #11 in this thread that deal with the very same problem.

                        Comment

                        • boobikins
                          New Member
                          • May 2009
                          • 5

                          #27
                          Stuck

                          Code:
                          <HTML>
                          <HEAD>
                          <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>
                          
                          <SCRIPT language="JavaScript">
                          
                          
                          
                          
                          function isAlreadySelected(newSelection, previousSelections)
                          
                          for (var index = 1; index <= previousSelections.length;index = index+ 1)
                          
                          {
                          	if (newSelection == previousSelections)
                          	{
                          	return true;
                          	}
                          	
                          }	
                          
                          
                          function selectNumbers(highNumber, numberToSelect) 
                          {
                          
                          var selectedArray = new Array(numberToSelect);
                          
                          for (var index = 0; index < numberToSelect; index = index + 1)
                          		{
                          			selectedArray[index] = 0; 
                          		}
                          
                          
                          for(var selected = 0; selected < numberToSelect; selected = selected + 1)
                          
                          { 
                              var anEntry = parseFloat(window.prompt('please select a number', '')); 
                            
                              while(isAlreadySelected(anEntry, selectedArray)) 
                                  { 
                                      anEntry = parseFloat(window.prompt('You have already entered that number!Please select another number.')); 
                                  } 
                            
                            
                                      selectedArray[index] = anEntry; 
                            
                            
                          }             
                            
                              return selectedArray 
                            
                          } 
                          
                          
                          
                          }
                          	
                          </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>
                          >

                          Hi guys, can't get this to work at all, I think I'm making progress, now feels like Im back to square one :( Please help!

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #28
                            As I've mentioned earlier in the thread, since this is coursework/homework, we can only guide you, not show you the answer. For that to work, you need to show what you've tried, what you expect to happen, what's happening instead, the exact problems you're having and any error messages. Just saying it doesn't work isn't enough. Help us to help you.

                            Comment

                            • boobikins
                              New Member
                              • May 2009
                              • 5

                              #29
                              Thanks for the advice, I cracked it!

                              Comment

                              Working...