[英]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.