Jump to content

Array Problem


paul2463

Recommended Posts

Hello,
I have written a function to compare information in an array that doesnt seem to work properly. here is the code:
[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;
  }[/code]
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:
[code]
if ((joinArray[i].toString())==(joinArray[j].toString()))

[/code]
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?
Link to comment
Share on other sites

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:
[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;
          break;
          }
          else{
          returnval = true;
          }
        }
    }
    return returnval;
  }[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.