This article example shares the specific code of JavaScript to count the number of times a character appears for your reference. The specific content is as follows Counts the number of times each character occurs in the user input string. For example: the input string is "abca12abbc121d", the output is: a appears 3 times, b appears 3 times, c appears 2 times, d appears 1 time, 1 appears 3 times, and 2 appears 2 times. Method 1 <script> var ary = "abca12abbc121d"; var obj = {}; var i = 0; ary1 = ary.toLocaleLowerCase(); for(i = 0; i < ary1.length; i++) { key = ary1[i]; if(obj[key]) { //The object contains this letter obj[key]++; } else { //The object does not have this letter, add the letter to the object obj[key] = 1; } } for(var key in obj) //Traverse this object { console.log(key + "This character appears" + obj[key] + "times"); } </script> The effect is as follows: This method is implemented using developer tools Method 2 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="sc/char-sunmmary.js"></script> <script> window.onload = function(){ var txtChars = document.getElementById("txt-chars"); var show = document.getElementById("show"); document.getElementById("btn-ok").onclick=function(){ var str=txtChars.value; var charTotal = total(str); // Descending charTotal.sort(function(a,b){ return (a.count-b.count)*-1; }) show.innerHTML="<ul>"; //slice(0,5) takes the first five for(var ct of charTotal.slice(0,5)){ show.innerHTML+="<li>"+ct.char+" appeared "+ct.count+" times</li>"; } show.innerHTML+="</ul>"; } } </script> </head> <body> <label for="txt-chars">Please enter a string of characters:</label> <input type="text" id="txt-chars" name=""/> <button id="btn-ok">OK</button> <p id="show"></p> </body> </html> char-sunmmary.js function Summary(char,count) { this.char=char; this.count=count; } // Pass a string and return the frequency of occurrence function total(str) { var result = []; for(var c of str){ var charObj = checkChar(result,c); if(charObj==null){ result.push(new Summary(c,1)); } else{ charObj.count++; } } return result; } // Search for the summary instance containing the character char in the array arr // If it exists, return the instance, otherwise return null function checkChar(arr,char) { var r=null; for(var c of arr){ if(c.char==char){ r=c; break; } } return r; } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of the perfect integration solution between Serv-U FTP and AD
>>: MySQL 5.7.17 installation and configuration graphic tutorial
Table of contents 1. What is DOM 2. Select elemen...
Table of contents Transaction Isolation Level Wha...
First, let's take a look at my basic developm...
This article shares the specific steps of install...
Transaction isolation level settings set global t...
Vue3.0 has been out for a while, and it is necess...
Recently, I used the webSocket protocol when work...
Sometimes when requesting certain interfaces, you...
Zero, Background I received a lot of alerts this ...
Table of contents Preface 1. Arrange the installa...
Table of contents Achieve results Available plugi...
Preface Backup is the basis of disaster recovery....
Html event list General Events: onClick HTML: Mous...
Table of contents 1. How to update in batches Con...
When nginx configures proxy_pass, the difference ...