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
+ 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 M150 TMA 5 : Programming : Task 3 - User selection of balls             function isAlreadySelected(newSelection, previousSelections)     {     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.     //it loops the number of times = to the full number of elements in  previousSelections     for(var index = 0; index < previousSelectons.length; index = index + 1)         {              if(newSelection == previousSelctions[index])              {              // if newselection matches any of the entries already in previousSelections then 'true' is returned                 return true;              }              else              {              // assuming it's a new unique value, then 'false' is returned                 return false;              }          }        }               function selectNumbers(highNumber, numberToSelect)     {       //create chosenNumbers as an array. (Size of the array is defined by user)     chosenNumbers = new Array[numberToSelect];     //create the variable nextStep ( for use later)     var nextStep     //For each entry in chosenNumbers     For (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)             {             //Set the element values to 0             chosenNumbers[index2] = 0;             }     //For each element in chosenNumbers     For (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)         {             //Prompt user to enter value stating Maximum & Minimum values              //parseFloat value entered             //Assign new value to the variable newNumber             var newNumber = parseFloat(window.prompt('please enter your number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));             //Call function isAlreadySelected,              while (isAlreadySelected (newNumber, chosenNumbers))             //Receive ‘True’ or False’ from isAlreadySelected                 {             //If isAlreadySelected returns ‘True’             //Prompt user “Sorry that number is not available, press OK to select again”                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' +                  'please enter another number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));                 }             //Else             //Assign value into next element of chosenNumbers                 chosenNumbers[index3] = newNumber;           //Return the array chosenNumbers         return chosenNumbers;         }           A test of the function selectNumbers()
May 18 '09 #1
Share this Question
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

 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 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 M150 TMA 5 : Programming : Task 3 - User selection of balls           /* 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)     {     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.     //it loops the number of times = to the full number of elements in  previousSelections     for(var index = 0; index < previousSelections.length; index = index + 1)         {              if(newSelection == previousSelections[index])              {              // if newselection matches any of the entries already in previousSelections then 'true' is returned                 return true;              }              else              {              // assuming it's a new unique value, then 'false' is returned                 return false;              }          }        }               /* The function prompts users to select numbers from a pool of balls     The function takes two arguments:         the highest number in the pool of balls         the number of balls to be selected       The user is prompted to make choices.      A check is made that a choice is not a duplicate.     The user is reprompted to make a choice if a selection has been selected already     The function returns an array of numbers selected by the user     */     function selectNumbers(highNumber, numberToSelect)     {       //create chosenNumbers as an array. (Size of the array is defined by user)     chosenNumbers = new Array(numberToSelect);     //create the variable nextStep ( for use later)     var nextStep     //For each entry in chosenNumbers     for (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)             {             //Set the element values to 0             chosenNumbers[index2] = 0;             }     //For each element in chosenNumbers     for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)         {             //Prompt user to enter value stating Maximum & Minimum values              //parseFloat value entered             //Assign new value to the variable newNumber             var newNumber = parseFloat(window.prompt('please enter your number between 1 & ' + highNumber, ''));             //Call function isAlreadySelected,              while (isAlreadySelected (newNumber, chosenNumbers))             //Receive ‘True’ or False’ from isAlreadySelected                 {             //If isAlreadySelected returns ‘True’             //Prompt user “Sorry that number is not available, press OK to select again”                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' +                  'please enter another number between' + numberToSelect[0] + ' & ' + numberToSelect.Length, ''));                 }             //Else             //Assign value into next element of chosenNumbers                 chosenNumbers[index3] = newNumber;         }         //Return the array chosenNumbers         return chosenNumbers;         }           A test of the function selectNumbers()
alternatively, any directions to useful resources would be helpful. May 18 '09 #5

 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

 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

 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 M150 TMA 5 : Programming : Task 3 - User selection of balls           /* 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)     {     // This loop iterates through the array previousSelections and checks each element against  the value of newselection.     //it loops the number of times = to the full number of elements in  previousSelections     for(var index = 0; index < previousSelections.length; index = index + 1)         {              if(newSelection == previousSelections[index])              {              // if newselection matches any of the entries already in previousSelections then 'true' is returned                 return true;              }              else              {              // assuming it's a new unique value, then 'false' is returned                 return false;              }          }        }               /* The function prompts users to select numbers from a pool of balls     The function takes two arguments:         the highest number in the pool of balls         the number of balls to be selected       The user is prompted to make choices.      A check is made that a choice is not a duplicate.     The user is reprompted to make a choice if a selection has been selected already     The function returns an array of numbers selected by the user     */     function selectNumbers(highNumber, numberToSelect)     {       //create chosenNumbers as an array. (Size of the array is defined by user)     chosenNumbers = new Array(numberToSelect);     //create the variable nextStep ( for use later)     var nextStep     //For each entry in chosenNumbers     for (var index2 = 0; index2 < numberToSelect; index2 = index2 + 1)             {             //Set the element values to 0             chosenNumbers[index2] = 0;             }     //For each element in chosenNumbers     for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1)         {             //Prompt user to enter value stating Maximum & Minimum values              //parseFloat value entered             //Assign new value to the variable newNumber             var newNumber = parseFloat(window.prompt('please enter your number between 1 & ' + highNumber, ''));             //update the array chosenNumbers with the entered number             chosenNumbers[index3] = newNumber;             //Call function isAlreadySelected,              while (isAlreadySelected (newNumber, chosenNumbers))             //Receive ‘True’ or False’ from isAlreadySelected                 {             //If isAlreadySelected returns ‘True’             //Prompt user “Sorry that number is not available, press OK to select again”                 newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' +                  'please enter another number between 1 & ' + highNumber, ''));                 }             //Else             //Assign value into next element of chosenNumbers                 chosenNumbers[index3] = newNumber;         }         //Return the array chosenNumbers         return chosenNumbers;         }           A test of the function selectNumbers()
May 18 '09 #11

 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

 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

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

 P: 10 Expand|Select|Wrap|Line Numbers function isAlreadySelected(newSelection, previousSelections)      {      for(var index = 0; index < previousSelections.length; index = index +1)          {               if(newSelection == previousSelections[index]) return true;           }         }   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

 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

 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 function isAlreadySelected(newSelection, previousSelections)      {      for(var index = 0; index < previousSelections.length; index = index +1)          {               if(newSelection == previousSelections[index]) return true;           }                if(newSelection != previousSelections[index]) return false;        }      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

 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

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

 P: 10 Expand|Select|Wrap|Line Numbers function isAlreadySelected(newSelection, previousSelections)       {       for(var index = 0; index < previousSelections.length; index = index +1)           {                if(newSelection == previousSelections[index]) return true;              }        }   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

 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 for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1){              var newNumber = parseFloat(window.prompt('please enter  your number between 1 & ' + highNumber,'')); //chosenNumbers[index3] = newNumber;//IHAVE COMMENTED THIS ONE YOU CAN HAVE THE RIGHT SELECTIONS NOW               while (isAlreadySelected(newNumber, chosenNumbers)){                  newNumber = parseFloat(window.prompt('Sorry, that number has been chosen already,' +                   'please enter another number between 1 & ' + highNumber, ''));              }                           chosenNumbers[index3] = newNumber;          }   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 function isAlreadySelected(newSelection, previousSelections)      {      for(var index = 0; index < previousSelections.length; index = index +1)          {                 if(newSelection == previousSelections[index]) return true;                return false;           }            }       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

 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

 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

 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

 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.