Friday, March 28, 2014

[jQuery][Sharing] Ways to check all values in an array are not same in jQuery.

My code is edited from Thariama in Stackoverflow. His solution is for “check all values in an array are same”. I changed that to checking to “check all values in an array are not same”, and apply to a webpage with 4 input box to test:
<!DOCTYPE html> <html><head>    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script></head><body>
<input type="text" id="slider_photo_1" /><input type="text" id="slider_photo_2" /><input type="text" id="slider_photo_3" /><input type="text" id="slider_photo_4" /><input type="submit" id="submit" /><script>var img_links = new Array();$("#submit").click(function() {    for (var i=0; i<4 ; i++){        var j =i+1;        img_links[i] = $("#slider_photo_"+j).val();    }    console.log(img_links[0]+" != "+img_links[1]+","+img_links[2]+","+img_links[3]+" ? "+check_nonequal_array_elements(img_links));});
function check_nonequal_array_elements(img_links){    if (img_links.length == 1 || img_links.length == 0) { return true;} //get one element only.    for (i=0;i<img_links.length;i++){       if (i > 0 && img_links[i] == img_links[i-1]) { return false;}    }    return true;}</script></body></html>
 
Result:
a != b,c,d ? true  <- Show all value are not same, In fact really the values are not samea != b,c,c ? false <- Show some values are duplicated.  In fact 3rd and 4th are duplicateda != c,c,c ? false <- Show some values are duplicated.  2nd, 3rd and 4th are duplicateda != c,e,c ? true  <- Show all value are not same. But 2nd and 4th are duplicated !
And I edited some to fix this problem :
<!DOCTYPE html> <html><head>    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script></head><body>
<input type="text" id="slider_photo_1" /><input type="text" id="slider_photo_2" /><input type="text" id="slider_photo_3" /><input type="text" id="slider_photo_4" /><input type="submit" id="submit" /><script>var img_links = new Array();$("#submit").click(function() {    for (var i=0; i<4 ; i++){        var j =i+1;        img_links[i] = $("#slider_photo_"+j).val();    }    console.log(img_links[0]+" != "+img_links[1]+","+img_links[2]+","+img_links[3]+" ? "+check_nonequal_array_elements(img_links));});
function check_nonequal_array_elements(img_links){    if (img_links.length == 1 || img_links.length == 0) { return true;} //get one element only.    for (i=0; i<img_links.length; i++){       for (j=1; j<img_links.length; j++){          if (i > 0 && img_links[i] == img_links[i-j]) {               return false;          }       }    }    return true;}</script></body></html>
Result:
a != b,c,d ? true  <- Show all value are not same, In fact really the values are not same
a != b,c,c ? false <- Show some values are duplicated.  In fact 3rd and 4th are duplicated
a != c,c,c ? false <- Show some values are duplicated.  2nd, 3rd and 4th are duplicated
a != c,e,c ? false  <- Show some values are duplicated.  2nd and 4th are duplicated

Reference:
http://stackoverflow.com/questions/6277476/check-all-values-in-an-array-are-same

No comments :

Post a Comment