Max and javaScript

I wanted to post this bit of javaScript that I am using in the Max programming environment. Using the Max js object to do javaScript is awesome! It helps me to accomplish very tedious tasks (tedious if I do them with native max objects) such as if() and for() loops.

I found the few tutorials on the js object to be a little lacking in details, such as how to distinguish between data from different inlets.

I’ve posted the code below, but the most helpful thing I discovered was that by coding:

</pre>if(inlet==0){}

would allow me to test which inlet I was receiving from.

</pre>
/*This is very simple code for subtracting the elements one a list 
from the corresponding elements in another list.
for my purposes I didn't need the lists to be the same length so.
*/
inlets = 2; //2 inlets one for each list 
outlets = 1;

var leftArray = [];
var rightArray= [];
var diffArray= [];


function list(){
	
	if (inlet==0) {	//this if statement detects if the inlet is the first
		leftArray = arrayfromargs(arguments); //this stores the list in an arrray
	}
				
	if (inlet==1) { //this detects if the inlet is the second
		rightArray = arrayfromargs(arguments);//stored in an array
	}
	
	for (var i=0; i<leftArray.length; i++) {
	
		var toStore = leftArray[i] - rightArray[i]; //find difference
	
		if (toStore<0) {
			toStore = toStore * -1;
		}	//store the differences in an array
		
		diffArray[i] = toStore;
	}
	
	bang(); //call the bang function
}
	
function bang() {
	outlet(0, diffArray); //print out the stored array
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s