我坚持使用一个应该计算字符串中大写字母的函数。但计数器的结果却是 0,我不知道我在哪里犯了错误。
const bigLettersCount = (str) => {
let result = 0;
for (let i = 0; i < str.length; i += 1) {
if (str[i].toUpperCase() === str[i]) {
result += i;
}
return result;
}
}
bigLettersCount('HeLLo')
最佳答案
您可以使用regex做同样的事情。
const str = 'HeLLo';
console.log(
(str.match(/[A-Z]/g) || '').length
)
关于javascript - 如何统计字符串中所有大写字符的个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60696944/