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

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

Detailed explanation of MySQL phantom reads and how to eliminate them

Table of contents Transaction Isolation Level Wha...

Tutorial on how to connect and use MySQL 8.0 in IDEA's Maven project

First, let's take a look at my basic developm...

Linux uses binary mode to install mysql

This article shares the specific steps of install...

Implementation of MySQL multi-version concurrency control MVCC

Transaction isolation level settings set global t...

Vue3.0 implements the encapsulation of the drop-down menu

Vue3.0 has been out for a while, and it is necess...

Detailed explanation of nginx reverse proxy webSocket configuration

Recently, I used the webSocket protocol when work...

How to configure nginx to return text or json

Sometimes when requesting certain interfaces, you...

Problems with index and FROM_UNIXTIME in mysql

Zero, Background I received a lot of alerts this ...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...

How to implement Mysql scheduled task backup data under Linux

Preface Backup is the basis of disaster recovery....

Summary of events that browsers can register

Html event list General Events: onClick HTML: Mous...

ReactHooks batch update state and get route parameters example analysis

Table of contents 1. How to update in batches Con...