473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

object error

153 100+
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

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>
  4.  
  5. <SCRIPT language="JavaScript">
  6.  
  7.  
  8.     /* Determines if a given ball has already been selected.
  9.  
  10.     The function takes two arguments: 
  11.          a whole number which is a new selection
  12.          an array which contains previous selections.
  13.  
  14.     The function returns:
  15.           true if the new selection exists in the previous selections
  16.           false otherwise
  17.     */
  18.     function isAlreadySelected(newSelection, previousSelections)
  19.     {
  20.  
  21.       for(var i = 0; i < previousSelectons.length; i = i + 1)
  22.             {
  23.                 if(newSelection == previousSelction[i])
  24.                     {
  25.                         return true;
  26.                     }
  27.                 else
  28.                     {
  29.                         return false;
  30.                     }
  31.             }
  32.  
  33.     }    
  34.  
  35.  
  36.  
  37.  
  38.     function selectNumbers(highNumber, numberToSelect)
  39.     {
  40.  
  41.  
  42.         anArray = new Array[numberToSelect];
  43.         for(int z = 0; z < numberToSelect; z = z + 1)
  44.             {
  45.                 anArray[z] = 0;
  46.  
  47.  
  48.             }
  49.  
  50.     for(var y = 0; y < numberToSelect; y = y + 1)
  51.  
  52. {
  53.     var anEntry = parseFloat(window.prompt('please enter your number', ''));
  54.  
  55.     while(isAlreadySelected(anEntry, anArray))
  56.         {
  57.             anEntry = parseFloat(window.prompt('You have already entered that number - please enter another number'));
  58.         }
  59.  
  60.  
  61.             anArray[y] = anEntry;
  62.  
  63.  
  64. }            
  65.  
  66.     return anArray
  67.  
  68.     }
  69.  
  70.  
  71.  
  72. </SCRIPT>
  73. </HEAD>
  74. <BODY>
  75.  
  76.     <STRONG>A test of the function selectNumbers()<BR></STRONG>
  77.     <FORM NAME = "lotteryForm">
  78.  
  79.     <INPUT TYPE = "button" NAME = "selectBalls"  VALUE ="Select your Numbers!"
  80.             ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
  81.     </FORM>
  82.  
  83. </BODY>
  84. </HTML>
  85.  
Apr 21 '09 #1
28 5283
Dormilich
8,658 Expert Mod 8TB
@brendanmcdonagh
well, objects are created using Functions, the correct call would be
Expand|Select|Wrap|Line Numbers
  1. anArray = new Array();
  2. // or
  3. anArray = new Array;
  4. // or (simplified)
  5. anArray = [];
see MDC – Array for a list of constructor options.
Apr 21 '09 #2
acoder
16,027 Expert Mod 8TB
Also, check your spellings in isAlreadySelected, e.g. lines 21 and 23.
Apr 22 '09 #3
DaiOz
16
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?

Expand|Select|Wrap|Line Numbers
  1.     function isAlreadySelected(newSelection, previousSelections)
  2.     {
  3.  
  4.  
  5.        for(var i = 0; i < previousSelections.length; i = i + 1)
  6.              {
  7.                  if(newSelection == previousSelections[i])
  8.                      {
  9.                          return true;
  10.                      }
  11.                  else
  12.                      {
  13.                          return false;
  14.                      }
  15.  
  16.  
  17.             }    
  18.  
  19.     }
May 9 '09 #4
acoder
16,027 Expert Mod 8TB
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.
May 10 '09 #5
DaiOz
16
Any hints how this can be done? Ive even resorted to the libary to help lol
May 10 '09 #6
acoder
16,027 Expert Mod 8TB
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 previousSelections. 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.
May 10 '09 #7
DaiOz
16
@acoder
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
May 10 '09 #8
DaiOz
16
I think i've nearly done it, solved the else problem but it will still only check the first 2 inputed numbers, help please :)

Expand|Select|Wrap|Line Numbers
  1. function isAlreadySelected(newSelection, previousSelections)
  2.     {
  3.  
  4.  
  5.        for(var i = 0; i < previousSelections.length; i = i++)
  6.              {
  7.                  if(newSelection == previousSelections[i])
  8.                      {
  9.                          return true;
  10.                      }
  11.                  else
  12.  
  13.                  if(newSelection != previousSelections[i])
  14.                     {
  15.                       return false;
  16.                      }
  17.  
  18.  
  19.  
  20.             }    
  21.  
  22.     }
  23.  
May 10 '09 #9
acoder
16,027 Expert Mod 8TB
That doesn't really solve the problem. Look at this code:
Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < anArray.length; i++) {
  2.     if (something == anArray[i]) return true;
  3. }
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.
May 11 '09 #10
DaiOz
16
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
May 11 '09 #11
Hi Im also doing th M150 course and really struggling to get the Task 2 to work..any advice anyone..help..


Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>M150 TMA 5 : Programming : Task 2 - Drawing numbered balls</TITLE>
  4.  
  5. <SCRIPT language="JavaScript">
  6.  
  7.  //and display it in an alert window  
  8.  
  9.     function getRandomNumber(aNumber) 
  10. {   
  11.         var getRandomNumber = Math.floor(Math.random()*49);
  12.  
  13.  
  14.     function lotteryDraw(highBall, numberToDraw)
  15.     {
  16.  
  17.         var numberPool = new Array(highBall);
  18.  
  19.         var sizeOfPool = highBall;
  20.  
  21.         var drawnNumbers = new Array(numberToDraw);
  22.  
  23.         for (var index = 0; index < highBall; index = index + 1)
  24.         {
  25.             numberPool[index] = index + 1; 
  26.         }
  27.  
  28.         for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1)
  29.         {
  30.             var randomNumber = getRandomNumber(sizeOfPool);
  31.  
  32.             drawnNumbers[drawnBall] = numberPool[getRandomNumber];
  33.  
  34.             numberPool[getRandomNumber] = numberPool[sizeOfPool - 1];
  35.  
  36.             sizeOfPool = sizeOfPool - 1;
  37.         }
  38.  
  39.         lotteryForm.drawnNumbersTextBox.value = drawnNumbers;
  40.     }
  41.  
  42.     }
  43.  
  44. </SCRIPT>
  45. </HEAD>
  46. <BODY>
  47.  
  48.     <STRONG>A test of the function lotteryDraw()<BR></STRONG>
  49.     <FORM NAME = "lotteryForm">
  50.     <P>
  51.     Total Number of Balls in Pool:
  52.     </P>
  53.     <P>
  54.     <INPUT TYPE = "text" NAME = "numberOfBallsTextBox"  SIZE ="2" MAXLENGTH = "2">
  55.     </P>
  56.     <P>
  57.     Number of Balls to Draw:
  58.     </P>
  59.     <P>
  60.     <!-- Note the SIZE attribute, whose value determines the size in characters of a text box and
  61.     the MAXLENGTH attribute, whose value constrains the length in characters of the allowed input -->
  62.     <INPUT TYPE = "text" NAME = "ballsToDrawTextBox" SIZE = "1" MAXLENGTH = "1" >
  63.     </P>
  64.  
  65.  
  66.     <!-- The RESET input type creates a button which, when clicked, resets a form to its original state -->
  67.     <INPUT TYPE = "reset" NAME = "resetButton" VALUE = "Clear Form">
  68.  
  69.     <!-- COMPLETE THE ONCLICK EVENT HANDLER TO INVOKE lotteryDraw() WITH ARGUMENTS TAKEN FROM THE FORM INPUT TEXT BOXES -->
  70.     <INPUT TYPE = "button" NAME = "drawBalls"  VALUE ="Draw the Balls!"
  71.                         ONCLICK = "lotteryDraw()" = "drawNumbersTextBox">
  72.  
  73.     <P>
  74.     Drawn Numbers:
  75.     </P>
  76.     <P>
  77.     <INPUT TYPE = "text" NAME = "drawnNumbersTextBox" SIZE = "30" MAXLENGTH = "30" >
  78.     </P>
  79.  
  80.     </FORM>
  81.  
  82. </BODY>
  83. </HTML>
Cheers
May 13 '09 #12
DaiOz
16
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
May 13 '09 #13
@DaiOz
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!!
May 13 '09 #14
DaiOz
16
Maybe take a look at the code below in particular,
Expand|Select|Wrap|Line Numbers
  1.  for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1)
  2.         {
  3.              var randomNumber = getRandomNumber(sizeOfPool);
  4.  
  5.              drawnNumbers[drawnBall] = numberPool[getRandomNumber];
  6.  
  7.              numberPool[getRandomNumber] = numberPool[sizeOfPool - 1];
  8.  
  9.              sizeOfPool = sizeOfPool - 1;
  10.  
May 13 '09 #15
@DaiOz
I think I have lost the plot!! everything seems goggly to me..lol.
May 13 '09 #16
@Oli2uok
Heeeeelllpppp!!!! is it just me!!!!
May 13 '09 #17
acoder
16,027 Expert Mod 8TB
@Oli2uok
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.
May 14 '09 #18
Expand|Select|Wrap|Line Numbers
  1.     <!-- COMPLETE THE ONCLICK EVENT HANDLER TO INVOKE lotteryDraw() WITH ARGUMENTS TAKEN FROM THE FORM INPUT TEXT BOXES -->
  2.     <INPUT TYPE = "button" NAME = "drawBalls"  VALUE ="Draw the Balls!"
  3.                         ONCLICK = "document.lotteryForm.drawnNumbersTextBox.value = lotteryDraw().value">
  4.  
  5.  
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
May 14 '09 #19
acoder
16,027 Expert Mod 8TB
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.
May 15 '09 #20
Expand|Select|Wrap|Line Numbers
  1. function getRandomNumber(aNumber) 
  2. {   
  3. var getRandomNumber = Math.floor(Math.random());
  4.  
  5.     return aNumber;
  6. }
  7.  
  8.     function lotteryDraw(highBall, numberToDraw) 
  9.     { 
  10.  
  11.         var numberPool = new Array(highBall); 
  12.  
  13.  
  14.         var sizeOfPool = highBall; 
  15.  
  16.  
  17.         var drawnNumbers = new Array(numberToDraw); 
  18.  
  19.  
  20.         for (var index = 0; index < highBall; index = index + 1) 
  21.         {     
  22.             //creates an array of all the numbers that can be possibly drawn !
  23.  
  24.             numberPool[index] = index + 1;  
  25.         } 
  26.  
  27.  
  28.         for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1) 
  29.         {
  30.  
  31.             var randomNumber = getRandomNumber(sizeOfPool); 
  32.  
  33.             drawnNumbers[drawnBall] = numberPool[randomNumber]; 
  34.  
  35.             numberPool[randomNumber] = numberPool[sizeOfPool - 1]; 
  36.  
  37.             sizeOfPool = sizeOfPool - 1; 
  38.         } 
  39.  
  40.         lotteryForm.drawnNumbersTextBox.value = drawnNumbers; 
  41.       } 
  42.  
  43. </SCRIPT>
  44. </HEAD>
  45. <BODY>
  46.  
  47.     <STRONG>A test of the function lotteryDraw()<BR></STRONG>
  48.     <FORM NAME = "lotteryForm">
  49.     <P>
  50.     Total Number of Balls in Pool:
  51.     </P>
  52.     <P>
  53.     <INPUT TYPE = "text" NAME = "numberOfBallsTextBox"  SIZE ="2" MAXLENGTH = "2">
  54.     </P>
  55.     <P>
  56.     Number of Balls to Draw:
  57.     </P>
  58.     <P>
  59.     <!-- Note the SIZE attribute, whose value determines the size in characters of a text box and
  60.     the MAXLENGTH attribute, whose value constrains the length in characters of the allowed input -->
  61.     <INPUT TYPE = "text" NAME = "ballsToDrawTextBox" SIZE = "1" MAXLENGTH = "1">
  62.     </P>
  63.  
  64.  
  65.     <!-- The RESET input type creates a button which, when clicked, resets a form to its original state -->
  66.     <INPUT TYPE = "reset" NAME = "resetButton" VALUE = "Clear Form">
  67.  
  68.     <!-- COMPLETE THE ONCLICK EVENT HANDLER TO INVOKE lotteryDraw() WITH ARGUMENTS TAKEN FROM THE FORM INPUT TEXT BOXES -->
  69.     <INPUT TYPE = "button" NAME = "drawBalls"  VALUE ="Draw the Balls!"
  70.                         ONCLICK = " var first = document.lotteryForm.ballsToDrawTextBox.value; var second = document.lotteryForm.numberOfBallsTextBox.value; lotteryDraw(first, second)">
  71.  
  72.     <P>
  73.     Drawn Numbers:
  74.     </P>
  75.     <P>
  76.     <INPUT TYPE = "text" NAME = "drawnNumbersTextBox" SIZE = "30" MAXLENGTH = "30" >
  77.     </P>
  78.  
  79.     </FORM>
  80.  
  81. </BODY>
  82. </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
May 15 '09 #21
acoder
16,027 Expert Mod 8TB
I think you've got the two paramters to lotteryDraw mixed up. Follow the code and the values input.
May 18 '09 #22
Hi,
Thanks for the reply I had realised what I had done about an hour after I had asked for help.

Cheers
May 18 '09 #23
acoder
16,027 Expert Mod 8TB
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.
May 18 '09 #24
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
Expand|Select|Wrap|Line Numbers
  1.  
  2.     function isAlreadySelected(newSelection, previousSelections) 
  3.     { 
  4.  
  5.     for(var index = 0; index < previousSelections.length; index = index + 1)  
  6.        {  
  7.             if(newSelection == previousSelections[index])  
  8.             {  
  9.  
  10.                 return true;  
  11.             }  
  12.             else  
  13.             {  
  14.  
  15.                 return false;  
  16.             }  
  17.         }  
  18.  
  19.     }
May 18 '09 #25
acoder
16,027 Expert Mod 8TB
See the previous posts from #4 - #11 in this thread that deal with the very same problem.
May 19 '09 #26
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>
  4.  
  5. <SCRIPT language="JavaScript">
  6.  
  7.  
  8.  
  9.  
  10. function isAlreadySelected(newSelection, previousSelections)
  11.  
  12. for (var index = 1; index <= previousSelections.length;index = index+ 1)
  13.  
  14. {
  15.     if (newSelection == previousSelections)
  16.     {
  17.     return true;
  18.     }
  19.  
  20. }    
  21.  
  22.  
  23. function selectNumbers(highNumber, numberToSelect) 
  24. {
  25.  
  26. var selectedArray = new Array(numberToSelect);
  27.  
  28. for (var index = 0; index < numberToSelect; index = index + 1)
  29.         {
  30.             selectedArray[index] = 0; 
  31.         }
  32.  
  33.  
  34. for(var selected = 0; selected < numberToSelect; selected = selected + 1)
  35.  
  36.     var anEntry = parseFloat(window.prompt('please select a number', '')); 
  37.  
  38.     while(isAlreadySelected(anEntry, selectedArray)) 
  39.         { 
  40.             anEntry = parseFloat(window.prompt('You have already entered that number!Please select another number.')); 
  41.         } 
  42.  
  43.  
  44.             selectedArray[index] = anEntry; 
  45.  
  46.  
  47. }             
  48.  
  49.     return selectedArray 
  50.  
  51.  
  52.  
  53.  
  54. }
  55.  
  56. </SCRIPT>
  57. </HEAD>
  58. <BODY>
  59.  
  60.     <STRONG>A test of the function selectNumbers()<BR></STRONG>
  61.     <FORM NAME = "lotteryForm">
  62.  
  63.     <INPUT TYPE = "button" NAME = "selectBalls"  VALUE ="Select your Numbers!"
  64.             ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
  65.     </FORM>
  66.  
  67. </BODY>
  68. </HTML>
  69.  
>

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!
May 20 '09 #27
acoder
16,027 Expert Mod 8TB
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.
May 21 '09 #28
Thanks for the advice, I cracked it!
May 22 '09 #29

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
2
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
2
by: Roby Eisenbraun Martins | last post by:
Hi, My name is Roby Eisenbraun Martins, I am a C++, VB and NET developer. I am working with a NET 2002 project right now and I am receiving this uncommon "OutOfMemory" error message when I try...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
2
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.