Page 1 of 1

Counting Variables in Server code

Posted: Thu Jul 17, 2014 8:32 pm
by Bad Addy

I have this simple script:

precode
var agree, unsure, disagree, i = 0;

var params = {};

params.criteria = { "question_id._id" : qid };

var results = Collection.query(dbid, "results", params);

console.log(results);

var count = results.length;

console.log('the count is ' + count);

while(i < count) {

if(results&#46;result === 1) {
agree++;
}
if(results&#46;result === 2) {
unsure++;
console&#46;log(unsure);
}
if(results&#46;result === 3) {
disagree++;
}

i++;

}
/code/pre

All this does, is cycle through the object 'checked' and adds each row that has the number 1,2 or 3. I have set the var as 0 from the outset, but when i run it, Server code trace states its NaN (Not A Number) when clearly it is and I am adding a 1 to it. Any reason why its doing this ?

This is the trace so you understand the preconsole&#46;logs/pre:

Image

Thanks


Counting Variables in Server code

Posted: Fri Jul 18, 2014 1:34 am
by Yurii Orishchuk

Hi Addy,

Thanks for your detailed description for this problem.

So i see in you code you just set set "0" value only "i" variable.

Please use following code to initialize your variables:

pre

var agree = 0;
var unsure = 0;
var disagree = 0;
var i = 0;

/pre

Regards.


Counting Variables in Server code

Posted: Fri Jul 18, 2014 7:33 am
by Bad Addy

That did it, I thought you could bulk set variables the why I did it, so all of them would equal 0.

Thank you though :)