0

I would like to change which array is being used by selecting it from a drop down box. The script then randomizes items in the given array.

The full code can be viewed here: http://codepen.io/anon/pen/Lqkdp/

HTML:

<select id="scale" name="scale" onchange="changeScale()">
<option value="array_Amaj">Amaj</option>
<option value="array_chromatic">Chromatic</option>
</select>

  <div id="scaleprint"></div>

JS:

var array_chromatic = ['A', 'A%23', 'Bb', 'B', 'C', 'C%23', 'Db', 'D', 'D%23', 'Eb', 'E', 'F', 'F%23', 'Gb', 'G', 'G%23', 'Ab'];

var array_Amaj = ['A', 'B', 'C%23', 'D', 'E', 'F%23', 'G%23'];

function changeScale()
{
  array_scale = document.getElementById("scale").value;
  document.getElementById("scaleprint").innerHTML = array_scale;
  array_chromatic = array_scale;
}

If the user selects 'Chromatic', the DIV called 'scaleprint' should update and the next part of the script should use that array to randomize and display the given note.

function renderKnuth()
        {
            array_chromatic.knuthShuffle();
            var audio = document.getElementById('sound');
      audio.src = 'http://lofiz.co.uk/gtr/dontfret/' + array_chromatic[0] + '.mp3';
      var str1 = array_chromatic[0]
      str2 = str1.replace("%23", '#');
            document.getElementById('knuth_data2').innerHTML = str2;

        }

1 Answer 1

1

After you set array_chromatic to a string

array_chromatic = array_scale

this

array_chromatic.knuthShuffle() 

is coming back undefined.

UPDATE

Change the values on your elements to numbers like this

<option value="0">Amaj</option>
<option value="1">Chromatic</option>

then change this

array_chromatic = array_scale;

to this

array_chromatic = my_arrays[array_scale];

and then add this

var my_arrays  = [array_Amaj, array_chromatic];

just below your array definitions.

Sign up to request clarification or add additional context in comments.

3 Comments

How would I counteract that? Do I need to move the array vars down, after the changeScale function? Or am I approaching this from the wrong angle?
Updated my answer to suggest a solution. I'm not sure that covers everything. Let me know if you need any other hints.
note that the order of the listing in my_arrays should match the <option> elements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.