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 Brief Introduction setInterval ...
1. Open port 2375 Edit docker.service vim /lib/sy...
environment Centos 6.6 MySQL 5.7 Install If the s...
Table of contents 1. Import files using script ta...
Table of contents Block-level functions Directly ...
Table of contents 1.1. Enable MySQL binlog 1.2. C...
Although Microsoft provides T4 templates, I find ...
Preface: In MySQL, the CONCAT() function is used ...
MySQL-Group-Replication is a new feature develope...
Overview One of the most powerful features of MyS...
Why optimize: With the launch of the actual proje...
sed is a character stream editor under Unix, that...
Recently, an error occurred while starting MySQL....
Seeing the recent popular WeChat tap function, I ...
Ansible is a new automated operation and maintena...