使用 JavaScript 统计字母中的环数
问题
我们需要编写一个 JavaScript 函数来输入一个英文字母的字符串。我们的函数应该统计字符串中出现的环数。
“O”、“b”、“p”、“e”、“A”等都有一个环,而“B”有两个环
示例
以下为代码示例 −
const str = 'some random text string';
function countRings(str){
const rings = ['A', 'D', 'O', 'P', 'Q', 'R', 'a', 'b', 'd', 'e', 'g', 'o', 'p', 'q'];
const twoRings = ['B'];
let score = 0;
str.split('').map(x => rings.includes(x)
? score++
: twoRings.includes(x)
? score = score + 2
: x
);
return score;
}
console.log(countRings(str));输出
7
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP