javascript string interpolation / template literals / template strings

    Misc

    bwagner
    Nov 20 2023 | 12:42 pm
    Using the js object I tried to employ string interpolation, but got the error: "js: midi2notename.js: Javascript SyntaxError: illegal character, line 11". I'm aware of the workaround to concatenate the generated string with "\n", but was wondering why there's this deviation from the standard [Template literals (Template strings) - JavaScript | MDN]
    Max Patcher
    In Max, select New From Clipboard.
    Show Text

    • tyler mazaika's icon
      tyler mazaika
      Nov 20 2023 | 12:47 pm
      Can't see your code (clipboard copy in Max doesn't get the external files). However, your issue is likely that Javascript in Max is ES5 (2009), so it does not have many modern niceties.
      Share
    • bwagner's icon
      bwagner's icon
      bwagner
      Nov 20 2023 | 12:56 pm
      Thank you, Tyler! For the record, below is the js code. Is the specific JavaScript version in Max documented somewhere?
      inlets = 1
      outlets = 1
      function msg_int(r) {
        	const names =
      		'C C# D D# E F F# G G# A A# B'.split(" ");
      	// appending "\n", because apparently, there's no way to do this in Max! See:
      	// https://cycling74.com/forums/carriage-returns-in-sprintf#reply-58ed1fd5c2991221d9cbf776
      	const pitch_octave = names[r % names.length] + (Math.floor(r/12)-2)
      	outlet(0, `${pitch_octave}\n`)
      }
    • tyler mazaika's icon
      tyler mazaika's icon
      tyler mazaika
      Nov 20 2023 | 1:52 pm
      It is hidden in sentence one of the Introduction article.
      https://docs.cycling74.com/max8/vignettes/javascriptinmax -> https://docs.cycling74.com/max8/vignettes/jsintro -> "The js and jsui objects run Javascript version 1.8.5 inside of Max."
    • bwagner's icon
      bwagner's icon
      bwagner
      Nov 20 2023 | 3:24 pm
      Thank you!