Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursevly compare values in string (comma separated) 2

Status
Not open for further replies.

lizok

Programmer
Jan 11, 2001
82
US
Hello all javascript gurus!
here is one i have a problem with:

i have a string with comma separated elements. Dynamic number of elements. Never know how many there are could be.

i need: compare them. make sure that none of them is equal to another one.

example: string (2,1,1,0)
there are 2 elements that are equal, i need to send a message to user ('sorry can't have equal elements')

example: string (2,1,3,0)
none of the elements are equal, i need to send a message to user ('perfect')

i think it should be recursive, but have no idea how to implement this. please help!!!


 
here's one way:

Code:
	<script type="text/javascript"><!--
	
	function s(a, b) {
	    return a - b;
	}
	
	function doTest(str) {
	    var prev;
		var valid = true;
	    var a = str.split(",");
		a.sort(s);
		prev = a[0];
		for( var i=1; i < a.length; i++ ) {
			if ( a[i] == prev ) {
				valid = false;
				break;
			}
			prev = a[i];
		}
		if ( valid ) alert( 'perfect' );
		else alert( 'imperfect' );
	}
	
	doTest('1,2,3,4,5');
	doTest('1,2,3,3,4');
	
	//--></script>



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
An ammendment to cory's script - this will check in case of any stray spaces in the comma delimited string, or for any possible numbers with leading 0's:

Code:
    <script type="text/javascript"><!--
    
    function s(a, b) {
        return a - b;
    }
    
    function doTest(str) {
        var prev;
        var valid = true;
        var a = str.split(",");
        a.sort(s);
        prev = [!]parseInt([/!]a[0][!], 10)[/!];
        for( var i=1; i < a.length; i++ ) {
            if ( [!]parseInt([/!]a[i][!], 10)[/!] == prev ) {
                valid = false;
                break;
            }
            prev = [!]parseInt([/!]a[i][!], 10)[/!];
        }
        if ( valid ) alert( 'perfect' );
        else alert( 'imperfect' );
    }
    
    doTest('1,2,3,4,5');
    doTest('1,2,3,3,4');
    doTest('1,2,00003,      3,4');
    
    //--></script>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
SW to yo momma
headbang.gif


-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top