How can I compare two shuffled strings?

后端 未结 5 973
盖世英雄少女心
盖世英雄少女心 2021-02-02 08:05

I have the following two strings:

var str1 = \"hello\";
var str2 = \"ehlol\";

How can I check whether both strings contain the same characters?

5条回答
  •  花落未央
    2021-02-02 09:01

    You can use a function for this purpose like sameChars function here-

    function myFunction()
    {
        var input_1 = document.getElementById('input_1').value;
        var input_2 = document.getElementById('input_2').value;
        var result = sameChars(input_1,input_2);
        document.getElementById("demo").innerHTML = result;
    }
    
    function sameChars(firstStr, secondStr)
    {
        var first = firstStr.split('').sort().join('');
        var second = secondStr.split('').sort().join('');
        return first.localeCompare(second)==0;
    }
    
    
    
    

提交回复
热议问题