0

I'm using JS inside MaxMSP

I would like to take a string output (of separated by comma single fibonacci numbers) and convert it to either an array or number, keeping the space and commas but getting ride of the "double quotes"

I've tried a bunch of stuff here but unfortunately I'm still banging my head against the wall.

autowatch = 1;

inlets = 2;
outlets = 2;


function msg_int(num){

  var a = 1; 
  var b = 0, temp;


  while (num >= 0){
    temp = a;
    a = a + b;
    b = temp;
    num--;
    outlet(0, b);


    var n = temp;
    digits = n.toString().replace(/(\d{1})/g,'$1, ')
    outlet(1, digits);


  }
    return b;


}

The Input number is 14 The first outlet(0) number is 610 The second outlet(1) is "6, 1, 0, "

I would like the second outlet to be 6, 1, 0,

JohnH
  • 2,713
  • 12
  • 21
sonoptik
  • 21
  • 2

2 Answers2

1

var result = '"6, 1, 0,"'.replace(/['"]+/g, '')
console.info(result);
Umair Sarfraz
  • 5,284
  • 4
  • 22
  • 38
  • Why not? This would work for all strings. Do you have an example of unknown strings? I'll add those to my answer. – Umair Sarfraz Jun 11 '19 at 22:09
  • this actually outputs "6, 1, 0," for me ` // // fibonacci sequence // autowatch = 1; inlets = 2; outlets = 2; function msg_int(num){ var a = 1; var b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; outlet(0, b); var result = '"6, 1, 0,"'.replace(/['"]+/g, '') outlet(1, result); } return b; } ` – sonoptik Jun 11 '19 at 22:19
  • What is `outlet()` ? If your code `outlet` is a number and not a function. – Umair Sarfraz Jun 11 '19 at 22:28
  • 1
    you can think of outlet like console.log in MaxMSP – sonoptik Jun 11 '19 at 22:42
0

It looks like you want outlet(1) to output a Max “list” as opposed to a symbol. outlet will handle that for you, but you need to hand it an array to achieve this. Here’s what it says in the docs:

If the argument to outlet is an array, it is unrolled (to one level only) and passed as a Max message or list (depending on whether the first element of the array is a number or string).

Knowing this, you need to convert digits into an array before passing it to outlet:

var n = temp;
var digits = n.toString(); // convert 610 to "610" (for example)
digits.split("");          // split "610" into ["6", "1", "0"]
outlet(1, digits);

Whether this a Max list or message depends on the type of the first element, so if you need a list of numbers (integers in your case), you could do something like this before passing it to outlet:

// map ["6", "1", "0"] to [6, 1, 0]
digits = digits.map(function (i) { return parseInt(i) });
swithinbank
  • 1,127
  • 1
  • 6
  • 15