存储字母表中每个字母在所需位置的字符串中出现的次数
Storing number of times each letter of the alphabet appears in a string in a desired location
我觉得我做得一点也不对...我走对了方向吗?我尝试实现循环来检查字符串"beg"中的每个字母是否与数组中的字母匹配。
"乞求"是已经为我的作业提供的文本
//
// ***(15) store the number of times the letter "a" appears in the string "beg" in 1st location;
// *** store the number of times the letter "b" appears in the string "beg" in 2nd location;
// *** store the number of times the letter "c" appears in the string "beg" in 3rd location;
// *** store the number of times the letter "d" appears in the string "beg" in 4th location;
// *** etc.
// *** show the 26 counts on one line separated by commas in the span block with id="answers15"
//
var alphaNum = [26];
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
];
for (i = 0; i < 26; i++) {
alphaNum[i] = 0;
}
for (i = 0; i < beg.length; j++) {
charNow = beg.substr(i, 1);
for (j = 0; j < 26; j++) {
if (charNow == alphabet[j])
alphaNum = alphaNum[j] + 1;
}
}
showAlpha = "";
for (i = 0; i < 26; i++) {
showAlpha = showAlpha + alphabet[i] + ": " + alphaNum[i] + "<br>"
}
ans15.innerHTML = showAlpha;
缺少
beg
,在alphabet
,缺少逗号,并在注释中注释了其他一些错误。
至少它是带有一些小更改的工作代码。
var beg = 'store the number of times the letter "a" appears in the string "beg" in 1st location;', // declaration missing
alphaNum = [], // empty array, not an array with one element 26
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
i, j, // declaration missing
charNow, // declaration missing
showAlpha = ""; // declaration missing
for (i = 0; i < 26; i++) {
alphaNum[i] = 0;
}
for (i = 0; i < beg.length; i++) { // should be i++
charNow = beg.substr(i, 1); // could be replaced by charNow = beg[i]
for (j = 0; j < 26; j++) {
if (charNow == alphabet[j]) { // adding some curly brackets
alphaNum[j] = alphaNum[j] + 1; // index missing
}
}
}
for (i = 0; i < 26; i++) {
showAlpha = showAlpha + alphabet[i] + ": " + alphaNum[i] + "<br>"
}
document.getElementById('ans15').innerHTML = showAlpha; // target missing
<div id="answers15"></div>
相关文章:
- 根据另一个数组对数组进行排序,包括位置和字符串[ES6]
- Javascript中的正则表达式,用于只有数字的字符串,并且不应在除数字以外的任何位置包含任何字符
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 在从索引位置返回的字符串中查找空白
- 为什么我在 webpack 中需要 html 文件时会得到一个位置字符串
- 如何在JavaScript中根据字符的位置提取子字符串
- 在光标位置的文本区域中插入一个字符串,并进行一些更改
- 通过位置栏更改查询字符串,AJAX加载的菜单不会't在没有硬刷新的情况下重新填充
- 如何在字符串中的特定位置插入变量
- javaScript更改字符串char's的位置
- 替换元素中字符串的所有位置
- javascript/谷歌地图:将位置对象转换为字符串
- 存储字母表中每个字母在所需位置的字符串中出现的次数
- 如何将字符串(包含位置)转换为数组
- Javascript 中的第一个下一个/上一个子字符串位置
- 如何根据子字符串位置在代码镜像中突出显示子字符串
- 根据字符串位置在谷歌地图中放置标记
- 使用Javascript查找字符串中的子字符串位置
- 鼠标处的字符串位置
- 查找 url 中的字符串位置并替换 url