Question

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

Was it helpful?

Solution

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=[];
  s=s.toLowerCase();
  for(i=0;i<pages.length; i++)
  {
     scores[i]=0;
     var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[i].lastIndexOf(" "))).toLowerCase();
     words=lowerCaseContents.split(" ");
     for(j=0;j<words.length;j++)
     {
        if(words[j].match(s))
        {
          scores[i] += 1;
        }
      }
  }
  return scores;
}
alert(findScoresC("w"));

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 bring scores[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.

var pages = [...];
function findScoresC(pages, s)
{
   ...
}
alert(findScoresC(pages, "w"));

OTHER TIPS

When your for loop exits, i is equal to words.length, which is one greater than the last index of indexScore. You are assigning nothing at all to scores[i] each time through.

It might be because you have a nested for loop with the same index variable.

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.

var pages=["[www.google.co.uk] This is the WWW. ","[www.yahoo.co.uk] This is also the WWW. "];

function findScoresC(s){ 
    var scores = [],
        words = [],
        wordScore;
        // indexScore = [] <- this doesn't seem necessary
    s = s.toLowerCase();

    // Make sure to use `var i` and not just `i`; otherwise, you are creating a global variable.
    for ( var i=0; i<pages.length; i++ ) {
        // Initialize me!
        scores.push(0);

        var lowerCaseContents = pages[i].substring(
            pages[i].indexOf("]") + 1, pages[i].lastIndexOf(" ")
        ).toLowerCase();
        words = lowerCaseContents.split(" ");

        // You were using `i` for this loop as well.  No can do.
        for ( var j=0; j<words.length; j++) {
            if ( words[j].match(s) ) {
               // wordScore = 1;  <- I don't know what you're using this for
               scores[i]++;
            }
        }
    };

    return scores;
}

console.log(findScoresC("w"));

here's a small function that counts how many times substring "subStr" occurs in "str", not counting [...]

function substrCount(str, subStr) {
   var str = str.replace(/\[.+?\]/g, "");
   var del = str.toLowerCase().split(subStr.toLowerCase()).join("");
   return (str.length - del.length) / subStr.length;
}

the rest is obvious ;)

// edit: this is how you apply this function to an array

 var someArray = ["whatever", "something", "else" ];
 var counter = [];
 for(var i = 0; i < someArray; i++)
      counter[i] = substrCount(someArray[i], "something");
      // or, to count only one match, i.e. just to test if a substring is present
      counter[i] = substrCount(someArray[i], "something") > 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top