Jump to content


Photo

Array Problem


  • Please log in to reply
1 reply to this topic

#1 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 09 July 2006 - 02:04 PM

Hello,
I have written a function to compare information in an array that doesnt seem to work properly. here is the code:
function compare(){
  var BPDates = PHPDateArray(); //function works fine
  var JSDates = JSDateArray(); //function works fine
  var joinArray = BPDates.concat(JSDates); //contains: 2006-07-12,2006-07-13,2006-07-13,2006-07-14 (middle 2 the same)
  var returnval;
  for (i=0; i<joinArray.length; i++) {
      for (j=i+1; j<joinArray.length; j++) {
          if (joinArray[i]==joinArray[j]) {
           returnval = false;
           }
           else{
           returnval = true;
           }
        }
     }
     return returnval;
  }
It keeps returning TRUE even though there are two slots with the same data in

I have tried adding some other code to it such as:
if ((joinArray[i].toString())==(joinArray[j].toString()))

but that doesnt seem to make it pick up the fact that the middle two bits of data are the same, as a date and a string in this case, can anyone spot my problem please?

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 10 July 2006 - 10:58 AM

hello Again

just in case anyone else was having this problem and also could not figure out what was causing the error. I have managed to sort it out and keep at least some of my hair in the process, the problem with the code I posted, WAS NOTHING, it was doing exactly what I asked it to, it checked the first two values, returnval = true (they are not the same) it then compared the next two, returnval = false( they are the same) then it checks the final two, returnval = true because they are different, do therefore the program checks everything and if the final two values are different it always returned true. what I needed wasa method for it to break out of the loop if a match was found returning false here is the way to do that:
function compare(){
  var BPDates = PHPDateArray(); //function works fine
  var JSDates = JSDateArray(); //function works fine
  var joinArray = BPDates.concat(JSDates); //contains: 2006-07-12,2006-07-13,2006-07-13,2006-07-14 (middle 2 the same)
  var returnval;
  for (i=0; i<joinArray.length; i++) {
      for (j=i+1; j<joinArray.length; j++) {
          if (joinArray[i]==joinArray[j]) {
           returnval = false;
           break;
           }
           else{
           returnval = true;
           }
        }
     }
     return returnval;
  }

notice the BREAK Statement after any values are the same and returnval was set to false, this kicks it out of the loops stopping any more comparisons that may return TRUE.

Well there we go, an answer to a problem now solved.

Paul
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users