Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

javascript - counting the same letters in a string but not in all lenght

I'm starting my adventure with javascript and i got one of first tasks. I must create function that count letter that most occur in string and write this in console. For example: var string = "assssssadaaaAAAasadaaab";

and in console.log should be (7,a) <--- the longest string is 7 consecutive identical characters (yes, before count i use .toLowerCase();, because the task requires it)

So far I have it and I don't know what to do next. Someone want to help?

var string = "assssssadaaaAAAasadaaab";
var string = string.toLowerCase();
function writeInConsole(){
  console.log(string);
  var count = (string.match(/a/g) || []).length;
  console.log(count);
}

writeInConsole();
question from:https://stackoverflow.com/questions/65642463/counting-the-same-letters-in-a-string-but-not-in-all-lenght

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm no sure if this works for you:

string source = "/once/upon/a/time/";
int count = 0;
foreach (char c in source) 
  if (c == '/') count++;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...