对于为什么这不能按预期工作,我真的处于停滞状态。它应该输出字母 s 在单词 Mississipi 中出现次数的数字 4。我是 javascript 新手,所以任何帮助将不胜感激。利亚姆
function countCharacters(target, input) { //Defining of the function, the parameters are target and input
var count = 0; //set count to 0 before the for loop
for (var i = 0; i < input.length; i = i + 1) { //the for loop that goes through each index of the input string
if (input.indexOf(i) == target) { //if a character in the string matches the target character
count = count + 1; //count adds 1 to its value
}
}
console.log(count); //When it breaks out of the loop, the amount of times the target was matched will be printed
return target, input; //return two parameters
}
console.log(countCharacters("s", "Mississippi"));
红糖糍粑
慕斯王
慕标琳琳
相关分类