使用 JavaScript 计算在字符串数组的字母表中占据位置的字母数
问题
我们需要编写一个JavaScript函数,该函数接受英文小写字母字符串数组。
我们的函数应该将输入数组映射到一个数组,该数组的对应元素是在索引中具有相同从1开始的索引与其在字母表中从1开始的索引的字符数的计数。
例如-
字符串'akcle'的计数将为3,因为字符'a'、'c'和'e'在字符串和英文字母中分别具有1、3和5的基于1的索引。
示例
以下是代码-
const arr = ["abode","ABc","xyzD"];
const findIndexPairCount = (arr = []) => {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
const res = [];
for (let i = 0; i < arr.length; i++) {
let count = 0;
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j].toLowerCase() === alphabet[j]) {
count++;
}
}
res.push(count);
}
return res;
};
console.log(findIndexPairCount(arr));输出结果[ 4, 3, 1 ]
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下