paul2463 Posted July 9, 2006 Share Posted July 9, 2006 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 inI 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? Quote Link to comment https://forums.phpfreaks.com/topic/14095-array-problem/ Share on other sites More sharing options...
paul2463 Posted July 10, 2006 Author Share Posted July 10, 2006 hello Againjust 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 Quote Link to comment https://forums.phpfreaks.com/topic/14095-array-problem/#findComment-55459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.