0

I am having trouble calling an arguement in the functin below.

function makeScales(size,note1,note2,note3,note4,note5,note6,note7,note8){
    for(var z=0; z<size; z++){
        if(semis=="Sharps"){
            notes = notes + " - " + noteSharp["note" + z];
        } else {
            notes = notes + " - " + noteFlat["note" + z];
        }
    }
}

I went through many debugging procedures and found that my error en-lies with noteSharp["note" + z] more specifically "note" + z . For example if i do console.log(noteSharp[note1]) i get the desired result and i set z to 7 so i can see that its populating appropriately but I get undefined for my results. Any help would be greatly appreciated.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
user3148364
  • 35
  • 1
  • 6
  • You can't access parameters using string concatenation. You can however access them via the `arguments` object by index. So start `z` at `1`, and use `noteSharp[arguments[z]]` – cookie monster Jan 01 '14 at 22:47
  • `noteSharp[note1]` and `noteSharp['note1']` are not the same. With your function prototype, there is no nice way of accomplishing what you're trying to do. – Blender Jan 01 '14 at 22:47
  • 1
    I'm afraid you've given us nothing to go on. What are all your note variables? What type of data are they? Where is `notes` initially defined? Are you aware that it's in global scope, and will also always be undefined by the end of this function assuming it wasn't defined somewhere previous? Please give some details/context. – Lawrence Jones Jan 01 '14 at 22:47

2 Answers2

1

"note" + z will give you a string like "note1" not an identifier.

If you want to access a piece of data by an index, then put it in an array, not in a variable with a number in its name.

For example:

function makeScales(size, notes) {
    //...
    notes = notes + " = " + noteSharp[ notes[z] ];
    //...
}

makeScales( "some size", [ "foo", "bar", "baz" ] );

Alternatively (and I wouldn't recommend this approach as it mixes different kinds of data in a single data structure), you can use the arguments object.

notes = notes + " - " + arguments[z];

arguments represents all the arguments passed to the function. The second argument (aka note1) will have the index 1 and so on.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thanks so much for the help everyone. I added an array with the arguements and then called them through the array which did the trick. – user3148364 Jan 01 '14 at 23:01
0

Your problem there is that noteSharp[note1] is using the value of the variable note1 as the key to lookup, whereas noteSharp["note" + z] results in a string of "note" followed by the value of z.

To reference a variable name via a string, use eval:

noteSharp[eval("note" + z)];

However, I would highly recommend not using eval. Others do too, but perhaps there is a counterpoint.

Please do this instead, otherwise Cthulhu might arise...

Pass note1, note2, ..., noteN as properties of an object. Therefore you can look up their values exactly as you desire, just one level deeper.

function makeScales(size, notes) {
    var z = 1;
    var note1 = notes["note" + z];
}

// Calling makeScales with our notes.
makeScales(10, {
    note1: 'c',
    note2: 'd',
    note3: 'e',
    // etc.
});
Community
  • 1
  • 1
Henry Blyth
  • 1,700
  • 1
  • 15
  • 23