# Using a string as parameter name

**URL:** <https://discourse.processing.org/t/using-a-string-as-parameter-name/10246>\
**Category:** Libraries\
**Created:** [April 13, 2019, 8:45pm UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246 "2019-04-13T20:45:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![b\_and\_z](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@b\_and\_z](https://discourse.processing.org/u/b_and_z)\
**Post date:** [April 13, 2019, 8:45pm UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/1 "2019-04-13T20:45:12Z")

</div>

Hi everyone!

I want to make a simple interactive sequencer for my homepage, but I’m running into a problem when trying to play the single notes.

```auto
let ton1;
let ton2;
let ton3;

let beats = 16;
let tonName = [];
let sequence = [];
let currentTone;

let pStepCnt;
let stepCntRaw = 0;
let stepCnt = 0;
let tempo = .2;

function preload() {
    ton1 = loadSound('assets/ton1.WAV');
    ton2 = loadSound('assets/ton2.WAV');
    ton3 = loadSound('assets/ton3.WAV');
}

function setup() {
    createCanvas(windowWidth, windowHeight);

    tonName[0] = 'ton1';
    tonName[1] = 'ton2';
    tonName[2] = 'ton3';

    //fill sequencer with random notes 1-3
    for (let i=0; i<beats; i++) {
        sequence[i] = tonName[floor(random(tonName.length))];
    }
 
}

function draw() {
    pStepCnt = stepCnt;
    stepCntRaw+=tempo;
    stepCnt = floor(stepCntRaw)%beats;

 
    currentTone = sequence[stepCnt];
    if (stepCnt!=pStepCnt&&currentTone) {
        currentTone.play();
    }
}

```

The “sequence” array is filled with strings containing the loadSound name. However, calling these strings to play the respective tone doesn’t work (see last line). The error message is “Uncaught TypeError: currentTone.play is not a function”. Does anyone have a solution?

Thanks!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2019, 11:14pm UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/2 "2019-04-13T23:14:44Z")

</div>

I guess the language chooses the type of the variable automatically

Sequence comes out as String but you want a note

Eg. ton1.play();

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2019, 11:20pm UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/3 "2019-04-13T23:20:33Z")

</div>

Maybe use a if statement to get from string to tone or a switch() clause or a hashMap

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [April 14, 2019, 7:38am UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/4 "2019-04-14T07:38:55Z")

</div>

why not just do something like

```auto
let beats = 16;
let tones = [];
let sequence = [];
let currentTone;

let pStepCnt;
let stepCntRaw = 0;
let stepCnt = 0;
let tempo = .2;

function preload() {
    tones = [
		loadSound('assets/ton1.WAV'),
		loadSound('assets/ton2.WAV'),
		loadSound('assets/ton3.WAV')
	];
}

function setup() {
    createCanvas(windowWidth, windowHeight);

    //fill sequencer with random notes 1-3
    for (let i=0; i<beats; i++) {
        sequence[i] = tones[floor(random(tones.length))];
    }
}

function draw() {
    pStepCnt = stepCnt;
    stepCntRaw+=tempo;
    stepCnt = floor(stepCntRaw)%beats;

 
    currentTone = sequence[stepCnt];
    if (stepCnt!=pStepCnt&&currentTone) {
        currentTone.play();
    }
}

```

why would you add another layer to the tones? unless it’s necessary i would just do as above and there are a few other things you could do to clean that up.

---

<div class="post-metadata">

**Author:** ![b\_and\_z](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@b\_and\_z](https://discourse.processing.org/u/b_and_z)\
**Post date:** [April 14, 2019, 8:05am UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/5 "2019-04-14T08:05:59Z")

</div>

yess I went with hotfooteds way, didn’t know you could fill an array with functions.  
What other things could I clean up? The thing is this is just a stripped down code, what I’m actually trying to do needs some more steps.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 14, 2019, 8:10am UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/6 "2019-04-14T08:10:25Z")

</div>

In the array there are no functions, just sound objects

The objects possess functions like play

The loadsounds just return objects into the array

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 14, 2019, 1:22pm UTC](https://discourse.processing.org/t/using-a-string-as-parameter-name/10246/7 "2019-04-14T13:22:14Z")

</div>

> [@hotfooted](#):
>
> `sequence[i] = tones[floor(random(tones.length))];`

p5js’ **random()** is overloaded to accept an array argument as well: 🕶

> **[reference | p5.js](https://p5js.org/reference/#/p5/random)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

So that whole loop can be rewritten more succinctly like this instead: ✍  
`for (let i = 0; i < beats; sequence[i++] = random(tones));`

Or like this too, assuming the array is still empty (its _length_ = 0) of course: 😉  
`for (let i = 0; i < beats; ++i) sequence.push(random(tones));`
