Odd Error (Function not Defined)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AB3004
    New Member
    • May 2009
    • 14

    Odd Error (Function not Defined)?

    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!

    Code:
    <HTML>
    <HEAD>
    <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>
    
    <SCRIPT language="JavaScript">
    
    	
    	
    	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;
    		}
    	
    	
    </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>
  • AB3004
    New Member
    • May 2009
    • 14

    #2
    I just realised I was missing the top level '}' bracket under that function.

    Have added it, but still no joy.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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 previousSelecti ons

      Comment

      • AB3004
        New Member
        • May 2009
        • 14

        #4
        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)

        Comment

        • AB3004
          New Member
          • May 2009
          • 14

          #5
          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 isAlreadySelect ed, 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 isAlreadySelect ed?
          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

          Code:
          while (isAlreadySelected (newNumber, chosenNumbers))
          I'm just stuck becasuse I'm not sure how JS utilises the True/False that gets returned from isAlreadySelect ed

          Any thoughts ...?

          [code=Javascript]
          <HTML>
          <HEAD>
          <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>

          <SCRIPT language="JavaS cript">


          /* 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 isAlreadySelect ed(newSelection , previousSelecti ons)
          {
          // This loop iterates through the array previousSelecti ons and checks each element against the value of newselection.
          //it loops the number of times = to the full number of elements in previousSelecti ons
          for(var index = 0; index < previousSelecti ons.length; index = index + 1)
          {
          if(newSelection == previousSelecti ons[index])
          {
          // if newselection matches any of the entries already in previousSelecti ons 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(h ighNumber, numberToSelect)
          {

          //create chosenNumbers as an array. (Size of the array is defined by user)
          chosenNumbers = new Array(numberToS elect);
          //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(wind ow.prompt('plea se enter your number between 1 & ' + highNumber, ''));
          //Call function isAlreadySelect ed,
          while (isAlreadySelec ted (newNumber, chosenNumbers))
          //Receive ‘True’ or False’ from isAlreadySelect ed
          {
          //If isAlreadySelect ed returns ‘True’
          //Prompt user “Sorry that number is not available, press OK to select again”
          newNumber = parseFloat(wind ow.prompt('Sorr y, 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;


          }
          </SCRIPT>
          </HEAD>
          <BODY>

          <STRONG>A test of the function selectNumbers() <BR></STRONG>
          <FORM NAME = "lotteryFor m">

          <INPUT TYPE = "button" NAME = "selectBall s" VALUE ="Select your Numbers!"
          ONCLICK = "var selection = selectNumbers(1 0,5); window.alert('Y ou selected: ' + selection);">
          </FORM>

          </BODY>
          </HTML>
          [/code]







          alternatively, any directions to useful resources would be helpful.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by AB3004
            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!
            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

            Comment

            • AB3004
              New Member
              • May 2009
              • 14

              #7
              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!

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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()

                Comment

                • AB3004
                  New Member
                  • May 2009
                  • 14

                  #9
                  Originally posted by Dormilich
                  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()


                  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)

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by AB3004
                    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.

                    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)

                    Comment

                    • AB3004
                      New Member
                      • May 2009
                      • 14

                      #11
                      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.


                      [CODE= Javascript ]
                      <HTML>
                      <HEAD>
                      <TITLE>M150 TMA 5 : Programming : Task 3 - User selection of balls</TITLE>

                      <SCRIPT language="JavaS cript">


                      /* 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 isAlreadySelect ed(newSelection , previousSelecti ons)
                      {
                      // This loop iterates through the array previousSelecti ons and checks each element against the value of newselection.
                      //it loops the number of times = to the full number of elements in previousSelecti ons
                      for(var index = 0; index < previousSelecti ons.length; index = index + 1)
                      {
                      if(newSelection == previousSelecti ons[index])
                      {
                      // if newselection matches any of the entries already in previousSelecti ons 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(h ighNumber, numberToSelect)
                      {

                      //create chosenNumbers as an array. (Size of the array is defined by user)
                      chosenNumbers = new Array(numberToS elect);
                      //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(wind ow.prompt('plea se enter your number between 1 & ' + highNumber, ''));
                      //update the array chosenNumbers with the entered number
                      chosenNumbers[index3] = newNumber;
                      //Call function isAlreadySelect ed,
                      while (isAlreadySelec ted (newNumber, chosenNumbers))
                      //Receive ‘True’ or False’ from isAlreadySelect ed
                      {
                      //If isAlreadySelect ed returns ‘True’
                      //Prompt user “Sorry that number is not available, press OK to select again”
                      newNumber = parseFloat(wind ow.prompt('Sorr y, 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;


                      }
                      </SCRIPT>
                      </HEAD>
                      <BODY>

                      <STRONG>A test of the function selectNumbers() <BR></STRONG>
                      <FORM NAME = "lotteryFor m">

                      <INPUT TYPE = "button" NAME = "selectBall s" VALUE ="Select your Numbers!"
                      ONCLICK = "var selection = selectNumbers(1 0,5); window.alert('Y ou selected: ' + selection);">
                      </FORM>

                      </BODY>
                      </HTML>

                      [/CODE]

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by AB3004
                        I have made some changes since my earlier posts so please see below current version. (that might be where the problem lies)
                        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.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          upcoming error *g*: try to repeat the second value.

                          Comment

                          • AB3004
                            New Member
                            • May 2009
                            • 14

                            #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 isAlreadySelect ed 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

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              I knew you could make it. *smile*

                              Comment

                              Working...