Javascript 编写一个名为strLetterCount的函数,该函数接受一个字符串并返回一个新字符串,其中包含字符串中的每个字母及其计数
编写一个名为strLetterCount的函数,该函数接受一个字符串并返回一个新字符串,每个字符后跟它在字符串中出现的次数。返回的字符应与字符串的顺序相同,每个字符的唯一字母后跟它在字符串中出现的次数 到目前为止,我的代码是:Javascript 编写一个名为strLetterCount的函数,该函数接受一个字符串并返回一个新字符串,其中包含字符串中的每个字母及其计数,javascript,Javascript,编写一个名为strLetterCount的函数,该函数接受一个字符串并返回一个新字符串,每个字符后跟它在字符串中出现的次数。返回的字符应与字符串的顺序相同,每个字符的唯一字母后跟它在字符串中出现的次数 到目前为止,我的代码是: //write function format with argument function strLetterCount (str){ //initialize var to hold results for charAt let cha
//write function format with argument
function strLetterCount (str){
//initialize var to hold results for charAt
let charAt = '';
let count = 0;
for(let i = 0; i < str.length; i++){
str.charAt(i);
results = str.charAt(i);
for (let j = 0; j < str.length ; j++){
if(str[i] === str){
count++
results = str.charAt(i) + count++
}
}
return results;
}
}
strLetterCount('taco'); //'t1a1c1o1'
//function should pass a string as argument
//loop through the string and if a new char is entered store that
//loop through string again and count num of new char
//returns a new string push charAt and numOfOcc
//编写带参数的函数格式
函数strLetterCount(str){
//初始化var以保存charAt的结果
让charAt='';
让计数=0;
for(设i=0;i
它应该返回't1a1c101'的输出,但是,我只让它在字符串中循环一次,并返回't'的第一个值,但它不计算出现的次数?我无法确定在何处更改逻辑以保持发生计数?我猜您试图实现这样的目标 每个字母将以与输入字符串相同的出现次数出现在输出字符串中。我不确定这是您想要的,但这正是strLetterCount函数想要做的
function strLetterCount (str){
let results = ""
//initialize var to hold results for charAt
for(let i = 0; i < str.length; i++)
{
// Get current char and store it into the results
// We will add the count in a second loop
let charAt = str.charAt(i)
let count = 0
results += charAt
for (let j = 0; j < str.length ; j++)
{
if(str.charAt(j) === charAt)
{
count++
}
}
results += count
}
return results;
}
函数strLetterCount(str){
让结果=“”
//初始化var以保存charAt的结果
for(设i=0;i
假设您不需要每个字母的计数都位于其起始位置,即“started”可以等于“s1t2a1r1e1d1”而不是“s1t2a1r1t2e1d1”,那么这就可以实现
function strLetterCount (str){
// create an object to store each letter
const countObj = {}
// iterate over the string
for (let i = 0; i < str.length; i++) {
// if the letter isn't stored in the object, add it with a count of one
if (!countObj[str[i]]) {
countObj[str[i]] = 1;
} else {
// if the letter is already in the object, increment the count
countObj[str[i]]++;
}
}
// create a string to add the letter / count to
let outStr = "";
// iterate over the object to create the string
for (const letter in countObj) {
outStr += `${letter}${countObj[letter]}`;
}
// return the string
return outStr;
}
函数strLetterCount(str){
//创建一个对象来存储每个字母
常量countObj={}
//迭代字符串
for(设i=0;i
这将满足要求
function strLetterCount(str) {
// split the string to array
let strArr = str.split("");
// new array without duplicate letter
let strArrWithoutDuplicates = strArr.filter((value, idx, arr) => arr.indexOf(value) === idx);
let retString = "";
// loop through the "strArrWithoutDuplicates" array to find the number of times
// each elemet appears on the original array
strArrWithoutDuplicates.forEach(letter => {
retString += letter;
retString += strArr.filter((value) => (value === letter)).length;
});
return retString;
}
let a = strLetterCount('taco'); //'t1a1c1o1'
console.log(a)
非常感谢。代码能够成功运行,我非常欣赏伪代码;它帮助我更好地理解我错过了什么!我怎样才能接受这个答案?我不熟悉堆栈溢出。在答案旁边有一个“检查”按钮(如v),你可以点击它。当它变为绿色时,表示您接受了该答案。希望helpsHow能帮你去掉重复的字符吗?例如:如果你有“woof”这个词,我如何得到w1o2f1而不是w1o2o2f1?在
For I
循环中的返回
保证“循环”最多运行一次。。。这就是回报的工作原理