检查字符在字符串中使用次数的函数
function that checks the number of times a character is used in a string
我正在编写一个函数,它接受一个字符串作为参数,检查它的给定字符(在这种情况下说 "B"),然后 returns 一个反映数字的整数这个角色出现的次数。我知道这可以使用正则表达式等来完成,但我使用的教程到目前为止没有提到正则表达式。代码时间:
function countBs(string) {
var i = 0;
var n = 0;
var position = string.charAt(n);
while (i < string.length) {
if (string.charAt(n) == "B")
n += 1;
i++; //This line causes the following else statement to throw a syntax error. But it's the only way I can think of to have the loop continue iteration *while* checking for equivalence to "B"
else
i++;
return n;
}
}
然后用console.log(countBs("ABBA"));
检查
尝试用大括号括起来:
if (string.charAt(n) == "B")
{ n += 1;
i++;
}
else
需要前面的 if
,中间没有其他语句。 i++
在 if
之外。
你的代码很糟糕。
function countBs(string) {
var i = 0;
var n = 0;
// var position = string.charAt(n); // REMOVE--NOT NECESSARY
while (i < string.length) {
if (string.charAt(i) == "B") // i, NOT n
n++; // CONSISTENCY IN ADD-ONE SYNTAX
// i++; // INCREMENT ONCE BELOW
//else
i++;
}
return n; // MUST GO OUTSIDE THE LOOP
}
因此正确的代码是:
function countBs(string) {
var i = 0;
var n = 0;
while (i < string.length) {
if (string.charAt(i) == "B") n++;
i++;
}
return n;
}
使用 while
循环没有什么特别的问题,但是 for
会更自然:
function countBs(str) {
var n = 0;
for (var i = 0; i < str.length; i++) if (str[i]== "B") n++;
return n;
}
现代 JS
供您参考,在现代JS中,您可以避免循环和变量。首先,让我们写一个单独的检查函数:
function isB(c) { return c === 'B'; }
然后写
function countBs(str) {
return str . split('') . filter(isB) . length;
}
或者,使用 reduce
:
function countBs(str) {
return str.split('').reduce(function(cnt, c) {
return cnt + isB(c);
}, 0);
}
或者,尽管您说过不想使用正则表达式:
function countBs(str) {
return (str.match(/B/g) || []) . length;
}
如果您在 ES6 环境中编写,则使用数组理解
function countBs(str) {
return [for (c of str) if (isB(c)) c] . length;
}
这是我的答案
function countBs(Str)
{
let char = "B" ;
return String(Str).split(char).length - 1;
}
function countChar(Str, char)
{
return String(Str).split(char).length - 1;
}
我正在编写一个函数,它接受一个字符串作为参数,检查它的给定字符(在这种情况下说 "B"),然后 returns 一个反映数字的整数这个角色出现的次数。我知道这可以使用正则表达式等来完成,但我使用的教程到目前为止没有提到正则表达式。代码时间:
function countBs(string) {
var i = 0;
var n = 0;
var position = string.charAt(n);
while (i < string.length) {
if (string.charAt(n) == "B")
n += 1;
i++; //This line causes the following else statement to throw a syntax error. But it's the only way I can think of to have the loop continue iteration *while* checking for equivalence to "B"
else
i++;
return n;
}
}
然后用console.log(countBs("ABBA"));
尝试用大括号括起来:
if (string.charAt(n) == "B")
{ n += 1;
i++;
}
else
需要前面的 if
,中间没有其他语句。 i++
在 if
之外。
你的代码很糟糕。
function countBs(string) {
var i = 0;
var n = 0;
// var position = string.charAt(n); // REMOVE--NOT NECESSARY
while (i < string.length) {
if (string.charAt(i) == "B") // i, NOT n
n++; // CONSISTENCY IN ADD-ONE SYNTAX
// i++; // INCREMENT ONCE BELOW
//else
i++;
}
return n; // MUST GO OUTSIDE THE LOOP
}
因此正确的代码是:
function countBs(string) {
var i = 0;
var n = 0;
while (i < string.length) {
if (string.charAt(i) == "B") n++;
i++;
}
return n;
}
使用 while
循环没有什么特别的问题,但是 for
会更自然:
function countBs(str) {
var n = 0;
for (var i = 0; i < str.length; i++) if (str[i]== "B") n++;
return n;
}
现代 JS
供您参考,在现代JS中,您可以避免循环和变量。首先,让我们写一个单独的检查函数:
function isB(c) { return c === 'B'; }
然后写
function countBs(str) {
return str . split('') . filter(isB) . length;
}
或者,使用 reduce
:
function countBs(str) {
return str.split('').reduce(function(cnt, c) {
return cnt + isB(c);
}, 0);
}
或者,尽管您说过不想使用正则表达式:
function countBs(str) {
return (str.match(/B/g) || []) . length;
}
如果您在 ES6 环境中编写,则使用数组理解
function countBs(str) {
return [for (c of str) if (isB(c)) c] . length;
}
这是我的答案
function countBs(Str)
{
let char = "B" ;
return String(Str).split(char).length - 1;
}
function countChar(Str, char)
{
return String(Str).split(char).length - 1;
}