简体   繁体   English

写一个 JavaScript function 得到指定字符串中每个字母出现的次数

[英]Write a JavaScript function to get the number of occurrences of each letter in specified string

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “写一个 JavaScript function 来获取指定字符串中每个字母的出现次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:按字母顺序排列 - 所以如果一个字母与下一个字母相同,则计数器会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不一样时,它会记录字母,它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “编写一个 JavaScript 函数来获取指定字符串中每个字母出现的次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:字母顺序 - 所以如果一个字母与下一个字母相同,计数器就会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不相同时,它会记录该字母、它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “编写一个 JavaScript 函数来获取指定字符串中每个字母出现的次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:字母顺序 - 所以如果一个字母与下一个字母相同,计数器就会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不相同时,它会记录该字母、它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “编写一个 JavaScript 函数来获取指定字符串中每个字母出现的次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:字母顺序 - 所以如果一个字母与下一个字母相同,计数器就会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不相同时,它会记录该字母、它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “编写一个 JavaScript 函数来获取指定字符串中每个字母出现的次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:字母顺序 - 所以如果一个字母与下一个字母相同,计数器就会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不相同时,它会记录该字母、它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

"Write a JavaScript function to get the number of occurrences of each letter in specified string." “编写一个 JavaScript 函数来获取指定字符串中每个字母出现的次数。” I've tried this way, but all my outputs are 0 and I really don't get why.我试过这种方式,但我所有的输出都是 0,我真的不明白为什么。

My idea was: Alphabetic order - so if one letter is the same with the next one, the counter increases.我的想法是:字母顺序 - 所以如果一个字母与下一个字母相同,计数器就会增加。 When it isn't the same, it logs the letter, how many times it appears and it resets the counter.当它不相同时,它会记录该字母、它出现的次数并重置计数器。

By the way, I don't know how to make it read the letters which appear only once.顺便说一句,我不知道如何让它读取只出现一次的字母。 Can you help?你能帮我吗?

 function count(string) { let string1 = string.split("").sort().join(""); let counter = 0; for (let i = 0; i < string.length; i++) { if (string1[i] == string[i + 1]) { counter++; } else { console.log(string1[i] + " " + counter); counter = 0; } } } count("thequickbrownfoxjumpsoverthelazydog");

Hello People This is my first post in stack Over flow.大家好这是我在堆栈溢出中的第一篇文章。

This is the Shortest Code that i can suggest.这是我可以建议的最短代码。

function letterOccurence(string) { let countedString ={}; function letterOccurence(string) { let countedString ={};

for(let letter of string)

{
       countedString[letter] = (countedString[letter] || 0)+1; //This will count the occurrence of each letter 
}
return countedString;

} console.log(letterOccurence(("thequickbrownfoxjumpsoverthelazydog"))); } console.log(letterOccurence(("thequickbrownfoxjumpsoverthelazydog")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个接受字符串并打印出该字符串的每个字母的JavaScript函数? - How to write a JavaScript function that takes a string and print out each letter of that string? 在 JavaScript 中获取字符串中每个单词的首字母 - Get first letter of each word in a string, in JavaScript 如何使用javascript返回字符串中每个字母的数字 - how to return number for each letter in a string using javascript 字符串中子字符串出现的次数[Javascript] - Number of occurrences of a substring in a string [Javascript] 给字符串中的每个字母添加一个数字 - Add a number to each letter in a string 计算字符串中每个字符的出现次数 - Count number of occurrences for each char in a string 编写一个名为 strLetterCount 的 function,它接受一个字符串并返回一个新字符串,其中包含每个字母及其在字符串中的计数 - Write a function called strLetterCount which accepts a string and returns a new string with each letter and its count in the string Javascript 统计字符串中某个字符出现的次数 - Count the number of occurrences of a character in a string in Javascript 查看javascript数组中每个字符串的第一个字母 - Look at the first letter of each string in an array in javascript 在Javascript中的每个新字母之前拆分字符串 - Split string before each new letter in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM