XML Feeds

.

[javascript-dev] Providing default values when expected arguments not provided

Roby Steinmetzer roby at arts.lu
Sat Oct 27 15:36:15 MDT 2007


Salut Roald,

On 27 oct. 07, at 21:29, Roald Baudoux wrote:
> I am trying to built a random number generator with lower and upper  
> limits defined as arguments.
>
> I try to provide default values in case no argument is typed in  
> however the following script doesn't work. It returns a NaN as output.

You could initialize the min and max vars with your default values and  
then check for the number of arguments.
Something like this:


// Random number generator

// inlets and outlets
inlets = 1;
outlets = 1;

//global variables

var random_minimum = 0.;
var random_maximum = 1.;

if (jsarguments.length==2)
{
	random_minimum = jsarguments[1];
}
else if (jsarguments.length==3)
{
	random_minimum = jsarguments[1];
	random_maximum = jsarguments[2];
}
else if (jsarguments.length>3)
{
	post("error: wrong number of arguments");
	post();
}

var random_range = random_maximum - random_minimum;

// random generation upon bang message
function bang()
{
	outlet (0, ((Math.random() * random_range)+random_minimum));
}


More information about the javascript-dev mailing list