## js question from a newbie

Mar 27 2008 | 1:29 am
Hi i started this week to program a little bit with javascript . For starters i made a simple henon map ,which works ok , but now i'd like to be able to store these into two dimentional arrays and see if i can find the numbers that repeat to use them to trigger perhaps different sets of initial conditions .
So i started reading about arrays in javascript and in my first example (filling an array and printing it reversed) there is a 'undefined ' message poping up in max window. Could anybody please let me know whats going on ?
here is the simplistic code :
// printing and filling an array
editfontsize = 16 ;
// my inlets and outlets
inlets = 1 ; outlets = 1;
// number of elements var x ; var myarray = new Array () ;
function msg_float(r) {
if (inlet == 0) { x = r ; fillarray(x) ; myreverse();
} }
function fillarray(k) { var i ;
for (i = k ; i > 0 ; i--) { myarray[i] = i ; post (); post(myarray[i]) ; }
post(); post ("my array has a lenght of" , myarray.length) ; post();
} // trying to print a reversed array function myreverse() { var z = myarray.reverse() ; post (z) ; }

• Mar 27 2008 | 7:07 am
im pretty sure array.reverse() doesnt return the array (or a copy), it just reverses it in place.
try
myarray.reverse(); post(myarray);
also i noticed that your loop checks for > 0, and arrays start from 0, maybe you want >= 0 ?
i would usually count upwards from zero
for(i = 0;i { myarray[i] = i; }
• Mar 27 2008 | 8:11 am
On 27 mars 08, at 02:29, Greg wrote:
> So i started reading about arrays in javascript and in my first > example (filling an array and printing it reversed) there is a > 'undefined ' message poping up in max window. > Could anybody please let me know whats going on ?
> for (i = k ; i > 0 ; i--)
Let say your float message was 3.0, your i variable will first be 3, then 2 then 1 and the loop will stop. So myarray[0] will remain... "undefined". Arrays in JavaScript start at index 0.
Note that your array's size will never decrease. So if you send 6.0 to your code, then 3.0, myarray.length will still be 7.
> _____________________________
Patrick Delges
Centre de Recherches et de Formation Musicales de Wallonie asbl http://www.crfmw.be/max
• Mar 27 2008 | 12:27 pm
it's also true that with this script , i can't change the array length if i send a smaller float in the input .
One thing i noticed which also mdk mentioned , also in other scripts i tried to make, that the reverse works on the array itself , i would expect it to work on a copy.
Is there a specific method to copy an array in js or do i have to use a loop to do so ?
And another question regarding timing issues , in my henon map i trigger each iteration with a bang coming from an external object (ex train, metro) . Would i have any benefits (timewise) if i did this within the script using the task methods or is it the same thing ?
thanks again
cheers
Greg
• Mar 27 2008 | 2:57 pm
On 27 Mar 2008, at 13:27, Greg wrote:
> > Is there a specific method to copy an array in js or do i have to > use a loop to do so ?
myArr.slice(); is the preferred way i believe. vb
• Mar 27 2008 | 4:26 pm
hi Volker
indeed i just found out about it , i probably shouldn't ask questions non max specific .
thanks
Greek
• Mar 28 2008 | 8:54 am
Quote: phaethon wrote on Thu, 27 March 2008 13:27 ----------------------------------------------------
> it's also true that with this script , i can't change the array length if i send a smaller float in the input .
Note that you can set the length property of an Array instance. So you can set it to the right size!
p
• Mar 31 2008 | 9:08 pm
Sorry for the many questions but i am new to programming generally as well as specifically in javascript . This is a new problem i am came up with :
I am having an input list sent to a specific function where i save it as an array . The array is declared as a variable within the function , however it seems that i can access it through other functions as well. Is this normal behaviour ? I am wondering since this is part of a bigger program , what will happen if i have two arrays with same name . here is thecode :
editfontsize = 16 ; // trying to create a function that takes an input list and create a new array taking into account the specified weights var z = new Array() ; // the array where i copy the a value from an input list var x = new Array() ; // the array where i copy the b value form an input list var v = new Array(); // the array where i copy the weighting value from an input list
function sortit() // this is where i sort the input array into three distinct arrays { count1 = count2 = count3 = 0 ; // a local counter used to set the elements of the three arrays v.length = x.length = z.length = (inputweights.length / 3) // here i set the length of the three lists (perhaps not needed) var t = inputweights.slice(); // copy the initial list var i = 0; {for ( i = 0 ; i < t.length ; i ++) if (( i % 3) == 0) {z[count1] = t[i] count1++ ;} else {if ((i % 3) == 1) {x[count2] = t[i] count2++ ;} else {v[count3] = t[i] count3++ ;}}} post(); post("my a values are " , z) ; post() ; post(" my b values are " ,x) ; post(); post("my weigth values are " , v) ;
}
function bang () { sortit()}
function weights(a) // saves the input to the weight function into an array { var inputweights = new Array ; post() ; post("the list contains",arguments.length, "elements"); if (( arguments.length % 3) != 0) // checks to see whether we have the correct number of arguments ( a b weight) {post(); post(" wrong number of arguments in the list ") ; post() ;} else {for ( i = 0 ; i < arguments.length ; i++)
inputweights[i] = arguments[i]; post(); post(inputweights) ; post() ;} }