Javascript - increment index script

bernardo's picture

hello everyone again bouncing around javascript

/*
A simple script that takes two input values, sums them and returns the result.
 
Special keywords for defining input and output key types:
__boolean, __index, __number, __string, __image, __structure, __virtual
 
Note that the function input arguments are read-only.
*/
var i = 0;
function (__number index) main (__boolean up, __boolean down)
{
   i++;
   var result = new Object();
 
 
   if (up){
   result.index = i++;
   }
 
   else if (down){
   result.index = i--;
   }            
   return result;
}

my idea is to hit the up key and down key and increment and decrement the index can anyone help? somehow this does not work thanks bern.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

smokris's picture
Re: Javascript - increment index script

See also the Counter patch.

PreviewAttachmentSize
Counter.png
Counter.png19.48 KB

cwright's picture
Re: Javascript - increment index script

var i = 0;
var result = new Object();
function (__number index) main (__boolean up, __boolean down)
{
   if(up)
      ++i;
   if(down)
      --i;
   result.index = i;
   return result;
}

bernardo's picture
Re: Javascript - increment index script

those who know, know!

thank you.

dust's picture
Re: Javascript - increment index script

bernardo i think your code should work, with the exception of the initial increment. your allocating i to 0 above your function, which is fine, but then in the first line of your function, it says i++; this will bump up your index to 1 before you evaluate your conditionals. so your first increment will have the equivalence of 2 when it should be 1. just thought i would point that out if your getting unexpected values.

you could also just use one conditional.....

if(up==true&&down!=true){++i}else{--i};

bernardo's picture
Re: Javascript - increment index script

uau thanks dust. i was frying my brain for supper last night around that!. still i decided to keep the brain so that i could post here. thanks for the explanations. ++