我认为我根本没有做到这一点......我是朝着正确的方向前进的吗?我试图实现循环来检查字符串中是否有每个字母" 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="ans15"
//
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;
&#13;
答案 0 :(得分:0)
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="ans15"></div>