By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Odd Error (Function not Defined)?

P: 14
Hi, I'm currently working on this piece of code & unfortunately, whenever I run it, it states that selectNumbers isn't defined and it errors on Line 1 (?)

can anyone tell me why? I'm tearing my hair out!

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.     function isAlreadySelected(newSelection, previousSelections)
  10.     {
  11.     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.
  12.     //it loops the number of times = to the full number of elements in  previousSelections
  13.     for(var index = 0; index < previousSelectons.length; index = index + 1) 
  14.        { 
  15.             if(newSelection == previousSelctions[index]) 
  16.             { 
  17.             // if newselection matches any of the entries already in previousSelections then 'true' is returned
  18.                 return true; 
  19.             } 
  20.             else 
  21.             { 
  22.             // assuming it's a new unique value, then 'false' is returned
  23.                 return false; 
  24.             } 
  25.         } 
  26.  
  27.     }    
  28.  
  29.  
  30.  
  31.     function selectNumbers(highNumber, numberToSelect)
  32.     {
  33.  
  34.     //create chosenNumbers as an array. (Size of the array is defined by user)
  35.     chosenNumbers = new Array[numberToSelect];
  36.     //create the variable nextStep ( for use later)
  37.     var nextStep
  38.     //For each entry in chosenNumbers
  39.     For (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)
  40.             {
  41.             //Set the element values to 0
  42.             chosenNumbers[index2] = 0;
  43.             }
  44.     //For each element in chosenNumbers
  45.     For (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)
  46.         {
  47.             //Prompt user to enter value stating Maximum & Minimum values 
  48.             //parseFloat value entered
  49.             //Assign new value to the variable newNumber
  50.             var newNumber = parseFloat(window.prompt('please enter your number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));
  51.             //Call function isAlreadySelected, 
  52.             while (isAlreadySelected (newNumber, chosenNumbers))
  53.             //Receive ‘True’ or False’ from isAlreadySelected
  54.                 {
  55.             //If isAlreadySelected returns ‘True’
  56.             //Prompt user “Sorry that number is not available, press OK to select again”
  57.                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' + 
  58.                 'please enter another number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));
  59.                 }
  60.             //Else
  61.             //Assign value into next element of chosenNumbers
  62.                 chosenNumbers[index3] = newNumber;
  63.  
  64.         //Return the array chosenNumbers
  65.         return chosenNumbers;
  66.         }
  67.  
  68.  
  69. </SCRIPT>
  70. </HEAD>
  71. <BODY>
  72.  
  73.     <STRONG>A test of the function selectNumbers()<BR></STRONG>
  74.     <FORM NAME = "lotteryForm">
  75.  
  76.     <INPUT TYPE = "button" NAME = "selectBalls"  VALUE ="Select your Numbers!"
  77.             ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
  78.     </FORM>
  79.  
  80. </BODY>
  81. </HTML>
  82.  
May 18 '09 #1
Share this Question
Share on Google+
36 Replies


P: 14
I just realised I was missing the top level '}' bracket under that function.

Have added it, but still no joy.
May 18 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
there are quite a bit of errors

- selectNumbers() is not closed (as you already noticed)
- it's for (), not For ()
- an Array is initialized as new Array(length) (parenthesis, not square brackets)
- numberToSelect is not an array
- there are several typos in previousSelections
May 18 '09 #3

P: 14
Righty Ho, - Feel I may have rushed that somewhat.

thanks for the advice, I'll do some major revisions & come back.

(I hate the capitalisation issue)
May 18 '09 #4

P: 14
Ok, - I have made all the tweaks you pointed out (*looks sheepish*)
And it seems to be working,

Initially it wasn't iterating through to ask repeated number entries but that was easy enough to rectify.

My issue now is that it either isn't correctly calling isAlreadySelected, or it is and I'm not manipulating the 'true/false' output correctly.

I tried to obtain Firebug to do a stepthrough, but the installer is an .xpi file which I can't open. - Have checked Filext.com & that states it as a mozilla file, so I'm not sure whether it's not compatible with vista or what? It's Most confusing!

Anyway, could anyone point me towards what I need to do to correctly utilise the 'True/False' that is returned from isAlreadySelected?
Dealing with the returned elements from objects isn't something we've covered in great depth (I've checked)

I've tried adding "= True" both within & outside the outer brackets of the While loop on Line 73

Expand|Select|Wrap|Line Numbers
  1. while (isAlreadySelected (newNumber, chosenNumbers))
I'm just stuck becasuse I'm not sure how JS utilises the True/False that gets returned from isAlreadySelected

Any thoughts ...?

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.  
  19.  
  20.     function isAlreadySelected(newSelection, previousSelections)
  21.     {
  22.     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.
  23.     //it loops the number of times = to the full number of elements in  previousSelections
  24.     for(var index = 0; index < previousSelections.length; index = index + 1) 
  25.        { 
  26.             if(newSelection == previousSelections[index]) 
  27.             { 
  28.             // if newselection matches any of the entries already in previousSelections then 'true' is returned
  29.                 return true; 
  30.             } 
  31.             else 
  32.             { 
  33.             // assuming it's a new unique value, then 'false' is returned
  34.                 return false; 
  35.             } 
  36.         } 
  37.  
  38.     }    
  39.  
  40.  
  41.  
  42.     /* The function prompts users to select numbers from a pool of balls
  43.     The function takes two arguments:
  44.         the highest number in the pool of balls
  45.         the number of balls to be selected
  46.  
  47.     The user is prompted to make choices. 
  48.     A check is made that a choice is not a duplicate.
  49.     The user is reprompted to make a choice if a selection has been selected already
  50.     The function returns an array of numbers selected by the user
  51.     */
  52.     function selectNumbers(highNumber, numberToSelect)
  53.     {
  54.  
  55.     //create chosenNumbers as an array. (Size of the array is defined by user)
  56.     chosenNumbers = new Array(numberToSelect);
  57.     //create the variable nextStep ( for use later)
  58.     var nextStep
  59.     //For each entry in chosenNumbers
  60.     for (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)
  61.             {
  62.             //Set the element values to 0
  63.             chosenNumbers[index2] = 0;
  64.             }
  65.     //For each element in chosenNumbers
  66.     for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)
  67.         {
  68.             //Prompt user to enter value stating Maximum & Minimum values 
  69.             //parseFloat value entered
  70.             //Assign new value to the variable newNumber
  71.             var newNumber = parseFloat(window.prompt('please enter your number between 1 & ' + highNumber, ''));
  72.             //Call function isAlreadySelected, 
  73.             while (isAlreadySelected (newNumber, chosenNumbers))
  74.             //Receive ‘True’ or False’ from isAlreadySelected
  75.                 {
  76.             //If isAlreadySelected returns ‘True’
  77.             //Prompt user “Sorry that number is not available, press OK to select again”
  78.                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' + 
  79.                 'please enter another number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));
  80.                 }
  81.             //Else
  82.             //Assign value into next element of chosenNumbers
  83.                 chosenNumbers[index3] = newNumber;
  84.         }
  85.         //Return the array chosenNumbers
  86.         return chosenNumbers;
  87.  
  88.  
  89.     }    
  90. </SCRIPT>
  91. </HEAD>
  92. <BODY>
  93.  
  94.     <STRONG>A test of the function selectNumbers()<BR></STRONG>
  95.     <FORM NAME = "lotteryForm">
  96.  
  97.     <INPUT TYPE = "button" NAME = "selectBalls"  VALUE ="Select your Numbers!"
  98.             ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
  99.     </FORM>
  100.  
  101. </BODY>
  102. </HTML>
  103.  






alternatively, any directions to useful resources would be helpful.
May 18 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
@AB3004
it's compatible with Mozilla Firefox (on any OS), to use it in IE you need FireBug Lite (check out their website)

… more to come
May 18 '09 #6

P: 14
as regards my isue, - I think I found it...

I think I'm placing to much faith in my coding.

from working through it, on Line 74 it's passing through the array chosenNumbers full of zero's! , I need to be filling that with the selected numbers as is go.

Whoops. ...back to work!
May 18 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
I see 2 main problems
- the error prompt (that should be easy to solve)
- checking the input (if you press "cancel", then NaN (not a number) is added to your array). see typeof and isNaN()
May 18 '09 #8

P: 14
@Dormilich


Thanks again for your continued assistance

1> fixed

2> it does state tin the Asignment question that
you can assume the input will always be a valid number in the range and there will not be a need for the code to check this.
I have now got the blasted thing working-ish (see my previous post) except for it seems to think (even though all the elements were set to 0) that as the first number chosen, '1' was deemed as a duplicate? I then put 3,3,3,3 & it accepted them all in.

Therefore, it's almost working, I just need to squint & hopefully see what I'm missing.

I know it's here somewhere (lol)
May 18 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
@AB3004

I don't know what IE does, but Firefox won't allow duplicate entries. if I repeatedly enter the same number, it shows the error prompt.

you can assume the input will always be a valid number in the range and there will not be a need for the code to check this.
though this makes life easier now, in any real world code you need type checking (never underestimate the user's stupidity)
May 18 '09 #10

P: 14
Hmm, - I'm using FireFox. (V 3.0.10 () ( I never use IE.)

as regards your other point, I totally agree, however on one of our earlier assignments I got marked down for adding extra functionality, was told to stick specifically to the specifications given.

I don't understand why it works for you & not for me! on mine, it seems to dislike the first number entered, then accepts any after (except the first number selected).

I have made some changes since my earlier posts so please see below current version. (that might be where the problem lies)

Thanks again for your continued efforts. they are very appreciated my friend.


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.  
  19.  
  20.     function isAlreadySelected(newSelection, previousSelections)
  21.     {
  22.     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.
  23.     //it loops the number of times = to the full number of elements in  previousSelections
  24.     for(var index = 0; index < previousSelections.length; index = index + 1) 
  25.        { 
  26.             if(newSelection == previousSelections[index]) 
  27.             { 
  28.             // if newselection matches any of the entries already in previousSelections then 'true' is returned
  29.                 return true; 
  30.             } 
  31.             else 
  32.             { 
  33.             // assuming it's a new unique value, then 'false' is returned
  34.                 return false; 
  35.             } 
  36.         } 
  37.  
  38.     }    
  39.  
  40.  
  41.  
  42.     /* The function prompts users to select numbers from a pool of balls
  43.     The function takes two arguments:
  44.         the highest number in the pool of balls
  45.         the number of balls to be selected
  46.  
  47.     The user is prompted to make choices. 
  48.     A check is made that a choice is not a duplicate.
  49.     The user is reprompted to make a choice if a selection has been selected already
  50.     The function returns an array of numbers selected by the user
  51.     */
  52.     function selectNumbers(highNumber, numberToSelect)
  53.     {
  54.  
  55.     //create chosenNumbers as an array. (Size of the array is defined by user)
  56.     chosenNumbers = new Array(numberToSelect);
  57.     //create the variable nextStep ( for use later)
  58.     var nextStep
  59.     //For each entry in chosenNumbers
  60.     for (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)
  61.             {
  62.             //Set the element values to 0
  63.             chosenNumbers[index2] = 0;
  64.             }
  65.     //For each element in chosenNumbers
  66.     for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)
  67.         {
  68.             //Prompt user to enter value stating Maximum & Minimum values 
  69.             //parseFloat value entered
  70.             //Assign new value to the variable newNumber
  71.             var newNumber = parseFloat(window.prompt('please enter your number between 1 & ' + highNumber, ''));
  72.             //update the array chosenNumbers with the entered number
  73.             chosenNumbers[index3] = newNumber;
  74.             //Call function isAlreadySelected, 
  75.             while (isAlreadySelected (newNumber, chosenNumbers))
  76.             //Receive ‘True’ or False’ from isAlreadySelected
  77.                 {
  78.             //If isAlreadySelected returns ‘True’
  79.             //Prompt user “Sorry that number is not available, press OK to select again”
  80.                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' + 
  81.                 'please enter another number between 1 & ' + highNumber, ''));
  82.                 }
  83.             //Else
  84.             //Assign value into next element of chosenNumbers
  85.                 chosenNumbers[index3] = newNumber;
  86.         }
  87.         //Return the array chosenNumbers
  88.         return chosenNumbers;
  89.  
  90.  
  91.     }    
  92. </SCRIPT>
  93. </HEAD>
  94. <BODY>
  95.  
  96.     <STRONG>A test of the function selectNumbers()<BR></STRONG>
  97.     <FORM NAME = "lotteryForm">
  98.  
  99.     <INPUT TYPE = "button" NAME = "selectBalls"  VALUE ="Select your Numbers!"
  100.             ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">
  101.     </FORM>
  102.  
  103. </BODY>
  104. </HTML>
  105.  
  106.  
May 18 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
@AB3004
rule of thumb: first test the value, then assign it.

you are assigning the value to your array and afterwards test if this value is present in the array, which will always evaluate to true.
May 18 '09 #12

Dormilich
Expert Mod 5K+
P: 8,639
upcoming error *g*: try to repeat the second value.
May 18 '09 #13

P: 14
Hail the Conquering Hero! ( cue Fanfare)

I sussed it out ( after only 4 houres of staring at it( lol)

The if then else statement in isAlreadySelected was wrong!

It was returning True/False after the first comparison, hence it was only ever comparing ther first value of the array.

by moving the 'else' instructions to the very end of the For loop, it meant that if it matched a value, - return True, otherise, move to the next. If it steps through all of them, Then, and only THEN does it return 'false'

Thank you ever so much for your help. I'm now onto Q 4 of 5.

I'll probably be back later!!

- AB
May 18 '09 #14

Dormilich
Expert Mod 5K+
P: 8,639
I knew you could make it. *smile*
May 18 '09 #15

P: 10
Expand|Select|Wrap|Line Numbers
  1. function isAlreadySelected(newSelection, previousSelections)
  2.      {
  3.      for(var index = 0; index < previousSelections.length; index = index +1) 
  4.         { 
  5.              if(newSelection == previousSelections[index]) return true;
  6.  
  7.         } 
  8.  
  9.      }
  10.  
Hi

I'm currently working on the same program and am having real difficulty with the above function. It seems to be working apart from when you enter the first number it thinks that that number has been chosen already. After this it doesn't let you enter the same number again.

I have tried adding else return false in braces out of braces and also tried adding another if statement with != instead of == ending in false but none of this seems to work.

I would really appreciate it if somebody could point me in the right direction.

Thank you
May 21 '09 #16

acoder
Expert Mod 15k+
P: 16,027
Look at #14. An explanation is provided for the answer.
May 22 '09 #17

P: 10
Hi

Yeah, I read that and could understand what they meant by move the if else statement to the end.

Would you be able to explain it a bit better for me, would much appreciate it.

Thanks:-)
May 22 '09 #18

acoder
Expert Mod 15k+
P: 16,027
It doesn't mean to actually move the whole part including the else otherwise the syntax would be invalid because you can't have an 'else' without an 'if'.

Think about it this way: you want to return true if you find a match and false if not. To find out if you haven't found one, you will need to match against all the values in the array, not just one. Once that is complete, then you can say that there are no matches and return false. That will therefore need to be after and outside the loop.
May 22 '09 #19

P: 10
Expand|Select|Wrap|Line Numbers
  1. function isAlreadySelected(newSelection, previousSelections)
  2.      {
  3.      for(var index = 0; index < previousSelections.length; index = index +1) 
  4.         { 
  5.              if(newSelection == previousSelections[index]) return true;
  6.  
  7.         }
  8.  
  9.              if(newSelection != previousSelections[index]) return false;
  10.  
  11.      }   
  12.  
I now have the code above which seems to work apart from 2 elements.

1. it still thinks the first entry is a duplicate.
2. it lets me enter numbers higher than the 10 which is what it is asking for.

I've looked through all the course books and can't find anything on this. I'm not sure it was covered.

Is my code starting to look right or am I completely of track.

Again, I really appreciate your help.
May 22 '09 #20

acoder
Expert Mod 15k+
P: 16,027
Why do you even need to match it against anything outside the loop? By that time, you already know that it hasn't matched anything.
May 22 '09 #21

P: 10
Does the 'else' statement need to be in there some where?

I'm just not seeing it.
May 22 '09 #22

acoder
Expert Mod 15k+
P: 16,027
No, no need for an else statement.
May 22 '09 #23

P: 10
Expand|Select|Wrap|Line Numbers
  1. function isAlreadySelected(newSelection, previousSelections)
  2.       {
  3.       for(var index = 0; index < previousSelections.length; index = index +1) 
  4.          { 
  5.               if(newSelection == previousSelections[index]) return true;
  6.  
  7.  
  8.          } 
  9.  
  10.     }
  11.  
Going by the above code do I just need to add one more line which is an 'if' statement.

Thank you.
May 22 '09 #24

omerbutt
100+
P: 638
@acoder
well sory if i am wrong but the problem could be solved by removing the code
chosenNumbers[index3] = newNumber;
from the for loop of the selectNumbers(highNumber, numberToSelect) which is appearing before the while loop
Expand|Select|Wrap|Line Numbers
  1. for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1){
  2.              var newNumber = parseFloat(window.prompt('please enter 
  3. your number between 1 & ' + highNumber,''));
  4. //chosenNumbers[index3] = newNumber;//IHAVE COMMENTED THIS ONE YOU CAN HAVE THE RIGHT SELECTIONS NOW 
  5.              while (isAlreadySelected(newNumber, chosenNumbers)){
  6.                  newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' + 
  7.                  'please enter another number between 1 & ' + highNumber, ''));
  8.              }             
  9.              chosenNumbers[index3] = newNumber;
  10.          }
  11.  
May 22 '09 #25

P: 3
Hi all,

I finally managed to get mine to work after hours of not being able to figure it out! It would seem that most of us studying M150 have found this difficult.

In my code, I HAVE used chosenNumbers[index3] = newNumber; as it is my understanding that, upon checking that the number has not already been selected, you will then need to enter it into the array and not before.

With regards to the isAlreadySelected function, which seems to be the one that's caught us all out, you will need to take out the else statement and just ask it return false (at the end of the for statement). Ignore what our textbooks tell us, we do not need the word else!

Hope this helps! I've found everyone's post incredibly helpful!

Cheers,

Nik
May 22 '09 #26

P: 10
Hi

I now have the following code for the isAlreadySelected function.

Expand|Select|Wrap|Line Numbers
  1. function isAlreadySelected(newSelection, previousSelections)
  2.      {
  3.      for(var index = 0; index < previousSelections.length; index = index +1) 
  4.         { 
  5.  
  6.              if(newSelection == previousSelections[index]) return true;
  7.  
  8.              return false;
  9.  
  10.         }
  11.  
  12.  
  13.  
  14.      }    
  15.  
But it is still not working. I have been working on this for over 12 hours now and still can't figure it out.

I would be really grateful if somebody could push me in the right direction.

Thanks:-)
May 22 '09 #27

P: 3
Hey,

Your return false is in the wrong place. What I think your telling the code to do there is: for each number, compare it to previous selections, return true if it matches, then return false, which doesn't make any sense. As acoder has previously mentioned, you want it to return false when it has completed the for loops and not returned true.

Does this help?

Nik
May 22 '09 #28

P: 10
Hi

Thanks for your reply, I'm not getting it I'm afraid, I think I have been working on it too long, think I'll take a break.

Could you tell me if I am missing a line of code or is all my code wrong, this would really help.

Thanks:-)
May 22 '09 #29

P: 3
Your not all wrong at all. Line 8 is in the wrong place, you need to move that down thats all.
May 22 '09 #30

Dormilich
Expert Mod 5K+
P: 8,639
@javaman
shut down the computer, take a walk in the park. this usually will get you relaxed and maybe enlightened back.
May 22 '09 #31

P: 10
Hi

I have move the return false line down

[removed code]

Unfortunately it still isn't working. It only seems to be the first entry which thinks it has already been entered, after that it works fine.

Any ideas

Thanks:-)
May 22 '09 #32

acoder
Expert Mod 15k+
P: 16,027
There must be a problem in your code where you call this function. Follow the values/variables you enter and set either using a debugger or adding a few alerts.
May 22 '09 #33

P: 10
That good news, at least I know the function is now right.

Where can I get a debugger?

Thanks again
May 22 '09 #34

acoder
Expert Mod 15k+
P: 16,027
I'd recommend Firebug (for Firefox), but a lite version is available for other browsers.
May 22 '09 #35

P: 10
Hi

I have cracked it!

I had 'chosenNumbers[index3] = newNumber;' in twice

I am so grateful to everyone here who has spared the time to help me through.

Thanks:-)
May 22 '09 #36

acoder
Expert Mod 15k+
P: 16,027
Glad you solved it yourself in the end. That's how it should be. Thanks for posting.
May 23 '09 #37

Post your reply

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