0

str1 に str2 が含まれるパーセンテージという点で、2 つの文字列を比較する必要があります。

str1="Hello wolrd"
str2="hello"

つまりdistance(str1, str2)、50%を返す必要があります..そのように。

4

2 に答える 2

0

私は数学者ではないので、このような魔法の公式を示すことはできませんが、この例では再帰を始めることができます - コードのコメントを注意深く見てください:

/* same_words array should hold the following:
   ["john", "bbc", "hot", "red,", "xyz,", "apples,", "and", "likes"]

   NOTES:
    - The more words you add, the less the percentage will be... that's how percent works.
    - milk and MILK are the same word, but:  MILK and MILK, aren't the same because of the comma (same goes for periods).
*/

var  jSmith =  'JOHN smith LIKES bananas, HOT chocolate, AND APPLES, nonsense, RED, 321, XYZ, BBC or npr',
jSmithArray =  jSmith.trim().toLowerCase().split(' '); //Makes an array of words for jSmith

var  jDoe =  'JOHN doe LIKES oranges, milk, AND APPLES, XYZ, 123, RED, green, HOT and... cnn sucks, BBC rocks',
jDoeArray =  jDoe.trim().toLowerCase().split(' '); //Makes an array of words for jDoe

var bothJohns = jSmithArray.concat(jDoeArray); //console.log(bothJohns);

var c = 0; //counter
var same_words = []; //collection container


//collects all the words that occur in both sentences
function collectSimilarWords(word) {
  same_words.push(word);
}


//The w parameter holds the word "john" for the 1st time.
//The fn parameter holds always the collectSimilarWords() function.
function recur(w, fn) {
  for (var p in jSmithArray) { //Every property of the jSmithArray Array holds a word string.
    if (w) {
       if (w == jSmithArray[p]) { //If the word we passed in as a parameter is the same word as one of the jSmithArray elements...
           fn( jSmithArray[p] ); //...then pass this word to the collectSimilarWords() function.
       }
       c += 1; //Increase c so we can move to...
       recur(jDoeArray[c], collectSimilarWords); //...the next word recursively.
    }
  }
}

//Call recur() and pass in the first word of the jDoeArray as the 1st param, and a function as the 2nd param.
recur(jDoeArray[c], collectSimilarWords);


function calcWhatever(samewords) { //Feel free to calculate however you want.
   var percent = ( (samewords.length * 100) / bothJohns.length ).toFixed(2);
   var msg = "Out of Smith\'s plus Doe's words (total: " + bothJohns.length + "), about " + percent + "% (or " + same_words.length + ") of them are found in both of their sentences!";
   return msg;
}

//Hopefuly setTimeout() will log the same_words array when the recursive function has finished.
window.setTimeout(function() {
    console.log(same_words);
    console.log(  calcWhatever(same_words)  );
}, 1000);

ループの代わりに、for in次のように for ループを簡単に使用できます。

for (var p = 0; p<jSmithArray.length; p++) { /* ... */ }
于 2012-04-16T22:32:20.047 に答える
-1

以下のコードで試してみてください。

<script>
var str1 = 'helloworld';
var str2 = 'hello';
var percent = 0;
var newstr = str1.search(str2);
if(newstr != -1){

    percent = (str2.length * 100)/str1.length;
   alert(percent+'%');

}else{
  alert(percent+'%');
}

</script>

私があなたの質問を理解していれば、これはあなたを助けると確信しています.

ありがとう。

于 2012-04-16T06:50:40.447 に答える