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 - <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>
-
28 5283 @brendanmcdonagh
well, objects are created using Functions, the correct call would be - anArray = new Array();
-
// or
-
anArray = new Array;
-
// or (simplified)
-
anArray = [];
see MDC – Array for a list of constructor options.
Also, check your spellings in isAlreadySelected, e.g. lines 21 and 23.
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? - function isAlreadySelected(newSelection, previousSelections)
-
{
-
-
-
for(var i = 0; i < previousSelections.length; i = i + 1)
-
{
-
if(newSelection == previousSelections[i])
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
-
-
}
-
-
}
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.
Any hints how this can be done? Ive even resorted to the libary to help lol
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.
@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
I think i've nearly done it, solved the else problem but it will still only check the first 2 inputed numbers, help please :) -
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;
-
}
-
-
-
-
}
-
-
}
-
That doesn't really solve the problem. Look at this 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.
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
Hi Im also doing th M150 course and really struggling to get the Task 2 to work..any advice anyone..help.. - <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
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
@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!!
Maybe take a look at the code below in particular, -
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;
-
@DaiOz
I think I have lost the plot!! everything seems goggly to me..lol.
@Oli2uok
Heeeeelllpppp!!!! is it just me!!!!
@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.
-
<!-- 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
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.
- 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
I think you've got the two paramters to lotteryDraw mixed up. Follow the code and the values input.
Hi,
Thanks for the reply I had realised what I had done about an hour after I had asked for help.
Cheers
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.
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 -
-
function isAlreadySelected(newSelection, previousSelections)
-
{
-
-
for(var index = 0; index < previousSelections.length; index = index + 1)
-
{
-
if(newSelection == previousSelections[index])
-
{
-
-
return true;
-
}
-
else
-
{
-
-
return false;
-
}
-
}
-
-
}
See the previous posts from #4 - #11 in this thread that deal with the very same problem.
- <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!
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.
Thanks for the advice, I cracked it!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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).
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |