char comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • latiara
    New Member
    • Mar 2007
    • 1

    char comparison

    how to compare the words in array?
    for example
    var words[1] = "blue"

    var words[2] = "eulb"

    var words[3] = "purple"


    How we find out that eulb and blue has same length and letters ? whereas elube doesnt have same length and letters . thanks
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Try this function (it is case insensitive):
    Code:
    function Compare(str1, str2){
    	var i = 0;
    	var regex = null;
    	var count1 = 0, count2 = 0;
    	if (str1.length != str2.length) return false;
    	for (i=0;i<str1.length;i++){
    		regex = new RegExp(str1.charAt[i],'g');
    		count1 = str1.replace(regex,"").length;
    		count2 = str2.replace(regex,"").length;
    		if (count1 != count2) return false;
    	}
    	return true;
    }
    It returns true if str1 and str2 have same length and contain same letters (and the same number of occurrences), otherwise false.

    Comment

    Working...