我真的停滞不前,为什么这不能按预期进行。它应输出密西西比词中字母s的出现次数的数字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"));