谁能看出我的 Javascript 有什么问题吗?
我编写了以下内容:
var pages=["[www.google.co.uk] This is the WWW. ","[www.yahoo.co.uk] This is also the WWW. "];
function findScoresC(s){
var scores=[];
var words=[];
var wordScore;
var indexScore=[];
s=s.toLowerCase();
for(i=0;i<pages.length; i++){
var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "))).toLowerCase();
words=lowerCaseContents.split(" ");
for(i=0;i<words.length;i++){
if(words[i].match(s)){
wordScore=1;
indexScore[i]=indexScore[i]+1};
scores[i] =indexScore[i]}};
return scores;
}
alert(findScoresC("w"));
该函数旨在返回一个数组(“scores”),其中数组的每个索引是在“pages”数组的每个索引中找到字符串 s 的次数,不包括正方形内的内容括号 - 但是,在每个单词中只找到一次字符串 s 。所以理想情况下,分数的第一个索引应该是 1,因为我用字母 w 调用了该函数,并且我只希望它在第一个页面索引中找到“WWW”的第一个 w - 如果这有意义的话。
到目前为止,我已经非常困惑了,所以我不知道为什么该函数返回“,,,,”而不是每个分数索引的数值 - 有什么想法吗?
谢谢
I have written the following:
var pages=["[www.google.co.uk] This is the WWW. ","[www.yahoo.co.uk] This is also the WWW. "];
function findScoresC(s){
var scores=[];
var words=[];
var wordScore;
var indexScore=[];
s=s.toLowerCase();
for(i=0;i<pages.length; i++){
var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "))).toLowerCase();
words=lowerCaseContents.split(" ");
for(i=0;i<words.length;i++){
if(words[i].match(s)){
wordScore=1;
indexScore[i]=indexScore[i]+1};
scores[i] =indexScore[i]}};
return scores;
}
alert(findScoresC("w"));
The function aims to return an array ("scores") where each index of the array is the number of times the string s is found in each index of the "pages" array, excluding what is inside the square brackets - however, only finding the string s once within each word. So ideally, the first index of scores would be 1, because I have called the function with the letter w, and i would only like it to find the first w of "WWW" in the first index of pages - if this makes sense.
I have confused myself pretty epically in getting this far, so I have no idea why the function is returning ",,,," rather than numerical values for each index of scores - any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当
for
循环退出时,i
等于words.length
,它比indexScore.每次都没有向
scores[i]
分配任何内容。When your
for
loop exits,i
is equal towords.length
, which is one greater than the last index ofindexScore
. You are assigning nothing at all toscores[i]
each time through.这可能是因为您有一个具有相同索引变量的嵌套 for 循环。
It might be because you have a nested for loop with the same index variable.
有几件事。我将内部索引的“i”替换为“j”。右括号后不需要加分号。说明后应该有一个分号(缺少几个分号)。
可能主要问题(在“i”问题之后)是分数[i]应该设置在内循环之外。如果将 cosing 括号分成单独的行,而不是像“
scores[i] =indexScore[i]}};
”,这会更清楚。事实证明,变量
indexScore
不是必需的。这使我可以将scores[i]
引入内部循环以直接累积单词点击量。最后,我更愿意将
pages
变量作为参数传递给函数,而不是假设它在全局空间中可用。如果可以的话,我倾向于避免全局变量。There were a few things. I replaced "i" with "j" for the inner index. You don't require a semicolon after a closing paren. You should have a semicolon after instructions (a couple were missing).
Probably the main issue (after the "i" issue) was that scores[i] should have been set outside the inner loop. This would have been clearer if the cosing parens had been separated out onto separate lines, instead of like "
scores[i] =indexScore[i]}};
".It turned out that the variable
indexScore
was not required. That allowed me to bringscores[i]
inside the inner loop to accumulate word hits directly.Finally, I would prefer to communicate the
pages
variable to the function as an argument than to assume that it is available in the global space. I tend to avoid globals if I can.这是你的功能修复。它返回
[1,1]
这似乎就是您想要的。我的笔记都在代码里了Here's you're function fixed. It returns
[1,1]
which appears to be what you were going for. My notes are in the code.这是一个小函数,用于计算子字符串“subStr”在“str”中出现的次数,不计算[...]
其余的很明显;)
//编辑:这就是将此函数应用于数组的方式
here's a small function that counts how many times substring "subStr" occurs in "str", not counting [...]
the rest is obvious ;)
// edit: this is how you apply this function to an array