Odd Error (Function not Defined)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaman
    New Member
    • May 2009
    • 10

    #16
    Code:
    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

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #17
      Look at #14. An explanation is provided for the answer.

      Comment

      • javaman
        New Member
        • May 2009
        • 10

        #18
        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:-)

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

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

          Comment

          • javaman
            New Member
            • May 2009
            • 10

            #20
            Code:
            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.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

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

              Comment

              • javaman
                New Member
                • May 2009
                • 10

                #22
                Does the 'else' statement need to be in there some where?

                I'm just not seeing it.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #23
                  No, no need for an else statement.

                  Comment

                  • javaman
                    New Member
                    • May 2009
                    • 10

                    #24
                    Code:
                    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.

                    Comment

                    • omerbutt
                      Contributor
                      • Nov 2006
                      • 638

                      #25
                      Originally posted by acoder
                      No, no need for an else statement.
                      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(h ighNumber, numberToSelect) which is appearing before the while loop
                      Code:
                      for (var index3 = 0; index3 < numberToSelect; index3 = index3 + 1){
                      			 var newNumber = parseFloat(window.prompt('please enter 
                      your number between 1 & ' + highNumber,''));
                      //chosenNumbers[index3] = newNumber;//[B]IHAVE COMMENTED THIS ONE YOU CAN HAVE THE RIGHT SELECTIONS NOW [/B]
                      			 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;
                      		 }

                      Comment

                      • NikW
                        New Member
                        • May 2009
                        • 3

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

                        Comment

                        • javaman
                          New Member
                          • May 2009
                          • 10

                          #27
                          Hi

                          I now have the following code for the isAlreadySelect ed function.

                          Code:
                          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:-)

                          Comment

                          • NikW
                            New Member
                            • May 2009
                            • 3

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

                            Comment

                            • javaman
                              New Member
                              • May 2009
                              • 10

                              #29
                              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:-)

                              Comment

                              • NikW
                                New Member
                                • May 2009
                                • 3

                                #30
                                Your not all wrong at all. Line 8 is in the wrong place, you need to move that down thats all.

                                Comment

                                Working...