user2964960
user2964960

Reputation: 29

Javascript record error

I am having a issue in adding records to arrays, this is where the problem is:

  if(score(web[i].content, pattern) > 0){
        scoresArray.push({"url:" + web[i].url + ", score:" + score(web[i].content, pattern)});
    }

the error report : SyntaxError: Unexpected token +

This is the array that this peace of code is reading from:

var web = [ {url : "www.lboro.ac.uk", content : "Loughborough University offers unidegree programmes and world class research." } , {url : "www.xyz.ac.uk", content : "An alternative University" } , {url : "www", content : "Yet another University" } ];

Upvotes: 1

Views: 58

Answers (1)

rninty
rninty

Reputation: 1082

You appear to be building one long string inside {}. The syntax for an object literal, on the other hand, is { key: expression, … }, where key is a string or an identifier.

So try this:

if(score(web[i].content, pattern) > 0) {
    scoresArray.push({
        url: web[i].url,
        score: score(web[i].content, pattern)
    });
}

You might want to keep a hold of score’s return value, too:

var item = web[i];
var itemScore = score(item.content, pattern);

if (itemScore > 0) {
    scoresArray.push({
        url: item.url,
        score: itemScore
    });
}

Upvotes: 2

Related Questions