JavaScript counts the number of times a character appears

JavaScript counts the number of times a character appears

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:
  • JavaScript counts the number of occurrences of each character in a string. Complete example
  • js code to count the number of characters that appear most often
  • Teach you how to use charAt() in JavaScript to count the most frequently appearing characters and their number of occurrences

<<:  Detailed explanation of the perfect integration solution between Serv-U FTP and AD

>>:  MySQL 5.7.17 installation and configuration graphic tutorial

Recommend

Detailed explanation of JavaScript timers

Table of contents Brief Introduction setInterval ...

How to use Docker plugin to remotely deploy projects to cloud servers in IDEA

1. Open port 2375 Edit docker.service vim /lib/sy...

How to install Mysql5.7 in Centos6

environment Centos 6.6 MySQL 5.7 Install If the s...

Javascript uses the integrity attribute for security verification

Table of contents 1. Import files using script ta...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

mysql-canal-rabbitmq installation and deployment super detailed tutorial

Table of contents 1.1. Enable MySQL binlog 1.2. C...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...

Summary of MySQL's commonly used concatenation statements

Preface: In MySQL, the CONCAT() function is used ...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

Database SQL statement optimization

Why optimize: With the launch of the actual proje...

Use the sed command to modify the kv configuration file in Linux

sed is a character stream editor under Unix, that...

How to solve the abnormal error ERROR: 2002 in mysql

Recently, an error occurred while starting MySQL....

Implementing WeChat tap animation effect based on CSS3 animation attribute

Seeing the recent popular WeChat tap function, I ...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...