var str =adadfdfseffserfefsefseeffffftsdg; Name a variable to place the given string var maxLength = 0; Name a variable to place the highest number of occurrences of the letter and initialize it to 0var result = '; Name a variable to place the result input while (str! = ") {//loop iteration begins and determine if the string is empty oldstr = str;//assigns the original string variable to the new variable getstr = STR.SUBSTR (0,1);//Sub with string Str method gets the first character (first letter) of eval (str = str.replace (/+getstr+/g, ')); Details such as supplemental if (Oldstr.length-str.length maxLength) {//Determine the length of the original string minus the substitution after the string length is greater than the maximum string length that occurred before maxLength = Oldstr.len Gth-str.length; Subtract two string lengths to get the maximum string length result = getstr + = + maxLength//returns the maximum string result (letters, occurrences)
}
}
Alert (Result)//Popup results
Supplement: eval (str = str.replace (/+getstr+/g, ')); maybe a lot of people want to write str = str.replace (/getstr/g, "), but the result will be wrong. Why, in this sentence, the expression matches the Getstr string, not the first letter that the getstr points to. By using the Eval method, you can avoid (first getstr get the first letter that you pointed to, connect str = str.replace (/+getstr+/g, ") with a string, and finally execute the code in eval, that is, the JavaScript code is interpreted before it is executed).
JS takes the most occurrences of the letters in a string