我写了以下内容:
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 - 如果这有意义的话。
走到这一步,我把自己搞糊涂了,所以我不知道为什么函数返回“,,,”,而不是每个分数索引的数值——有什么想法吗?
谢谢