Doing Calculations with arrays

O

outstretchedarm

I'm pretty noobish to javascript

I want to make algorithms that take selections from arrays and put them
together in new ways.

here is a simple array I set up for notes of a piano keyboard:

<html>
<body><script type="text/javascript">
var x
var note = new Array()
note[0] = "C"
note[1] = "Db"
note[2] = "D"
note[3] = "Eb"
note[4] = "E"
note[5] = "F"
note[6] = "Gb"
note[7] = "G"
note[8] = "Ab"
note[9] = "A"
note[10] = "Bb"
note[11] = "B"

for (x in note)
{
document.write(note[x] + "<br />")
}
</script></body>
</html>

what I want to do is:

1) enable the user to select a "starting note" (that is the key)

2) enable the user to select a scale, that is, a "path" through these
notes. If the user selects major, the program will, starting from the
selected starter note, choose thenext notes according to this pattern:

Root (the key selected) + 2 + 2 + 1 + 2 + 2 + 2 + 1

so if the user selects "C" and "major scale," it will print:
C, D, E, F, G, A, B, C

3) I want the program to "create" chords from the notes in the scale
generated.

like this:

I = 1 + 3 + 5 = C chord = C + E + G
II = 2 + 4 + 6 = D chord = D + F + A
III = 3 + 5 + 7 = E chord = E + G + B
IV = 4 + 6 + 1 = F chord = F + A + C
V = 5 + 7 + 2 = G chord = G + B + D
VI = 6 + 1 + 3 = A chord = A + C + E
VII= 7 + 2 + 4 = B chord = B + D + F

I need help with:

a) can somebody give me hints on how to code the algorithms that would
do this?

b) could somebody point me in the directions of a set of snippets that
might help me on this? Have mercy, I am a n00b, not begging for a
handout, but for help.

thanks
 
M

McKirahan

[snip]
what I want to do is:

1) enable the user to select a "starting note" (that is the key)

2) enable the user to select a scale, that is, a "path" through these
notes. If the user selects major, the program will, starting from the
selected starter note, choose thenext notes according to this pattern:

Root (the key selected) + 2 + 2 + 1 + 2 + 2 + 2 + 1

so if the user selects "C" and "major scale," it will print:
C, D, E, F, G, A, B, C

3) I want the program to "create" chords from the notes in the scale
generated.

like this:

I = 1 + 3 + 5 = C chord = C + E + G
II = 2 + 4 + 6 = D chord = D + F + A
III = 3 + 5 + 7 = E chord = E + G + B
IV = 4 + 6 + 1 = F chord = F + A + C
V = 5 + 7 + 2 = G chord = G + B + D
VI = 6 + 1 + 3 = A chord = A + C + E
VII= 7 + 2 + 4 = B chord = B + D + F

I need help with:

a) can somebody give me hints on how to code the algorithms that would
do this?

b) could somebody point me in the directions of a set of snippets that
might help me on this? Have mercy, I am a n00b, not begging for a
handout, but for help.

Here's a start. Watch for word-wrap.

<html>
<head>
<title>Notes.htm</title>
<script type="text/javascript">
var arr1 = new Array();
var arr2 = new Array();
function build() {
var note = "C,Db,D,Eb,E,F,Gb,G,Ab,A,Bb,B";
var html = "<form name='form1'>";
html += "Notes: <select name='Notes'>\n";
html += "<option value=''></option>\n";
var itm1 = note.split(",");
for (var i=0; i<itm1.length; i++) {
arr1 = itm1;
html += "<option value='" + i + "'>" + arr1 + "</option>\n";
}
var itm2 = (note+","+note+","+note).split(",");
for (var j=0; j<itm2.length; j++) {
arr2[j] = itm2[j];
}
html += "</select>\n";
html += "&nbsp;\n";
html += "Scale: <select name='Scale'>\n";
html += "<option value=''></option>\n";
// html += "<option value='1'>Minor</option>\n";
html += "<option value='2'>Major</option>\n";
html += "</select>\n";
html += "&nbsp;\n";
html += "<input type='button' value='Chords' onclick='chords()'>\n";
html += "</form>\n";
document.getElementById("html").innerHTML = html;
}
function chords() {
var form = document.form1;
var valu = form.Notes.options[form.Notes.selectedIndex].value;
// var scal = form.Scale.options[form.Scale.selectedIndex].value;
var next = parseInt(valu,10);
var what = arr1[next];
if (what == "") return;
var patt = [2,2,1,2,2,2,1];
for (var k=0; k<patt.length; k++) {
next += patt[k];
what += " " + arr2[next];
}
alert(what);
}
window.onload=build;
</script>
</head>
<body>
<div id="html"></div>
</body>
</html>


The "Scale" dropdown does nothing -- yet.

You lost me with:
 
V

VK

outstretchedarm said:
1) enable the user to select a "starting note" (that is the key)

2) enable the user to select a scale, that is, a "path" through these
notes. If the user selects major, the program will, starting from the
selected starter note, choose thenext notes according to this pattern:

Root (the key selected) + 2 + 2 + 1 + 2 + 2 + 2 + 1

so if the user selects "C" and "major scale," it will print:
C, D, E, F, G, A, B, C

I am no way a specialist of any kind in music, so I was going by the
formal description only (I hope I got it right). It may help to start -
given that someone else may propose a better starting point and/or
further steps.

<html>
<head>
<title>Notes</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

// The proper style in JavaScript is
// to end each statement with ;

var note = new Array();
note[0] = "C";
note[1] = "Db";
note[2] = "D";
note[3] = "Eb";
note[4] = "E";
note[5] = "F";
note[6] = "Gb";
note[7] = "G";
note[8] = "Ab";
note[9] = "A";
note[10]= "Bb";
note[11]= "B";


var scale = new Array();
scale[0] = new Array(2,2,1,2,2,2,1);
//scale[1] = ... etc.

function getGamma(k) {
// Create new array to store gamma
// and put the key in it right away:
var gamma = new Array(note[k]);

// That will be the pointer to peek up
// the notes from note array:
var ptr = k;

// For all notes in the given gamma...
for (var i=0; i<scale[k].length; ++i) {

// Add delta to the pointer to find the next position
ptr+= scale[k];

// If it went outside of defined array boundaries then
// bring it back
if (ptr >= note.length) {ptr-= note.length;}

// Add the pointed note to the results
gamma.push(note[ptr]);
}

// In string context array will be transformed
// into comma separated value, so quick'n'durty
// looking at the results
document.getElementById('out').innerHTML = gamma;
}
</script>
</head>

<body>
<button type="button" onClick="getGamma(0)">C</button>
<p id="out"> </p>
</body>
</html>
 
O

outstretchedarm

WOW!!!!! This is really awesome! I have been trying to do this for
days!

you don't know how grateful I am! i will study this code and try to
glean as much understanding as I can from it.

can someone point me in the direction of learning more about arrays,
since that is where I am doing most of my research?
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 12 Sep 2006 15:19:14 remote, seen in
McKirahan said:
Here's a start. Watch for word-wrap.

Word-wrap is YOUR responsibility.

Javascript should not be written in the manner that you write VBScript.
IMHO, neither should VBScript.


var html = "<form name='form1'>" +
"Notes: <select name='Notes'>\n" +
"<option value=''></option>\n";

should be better than
var html = "<form name='form1'>";
html += "Notes: <select name='Notes'>\n";
html += "<option value=''></option>\n";
var itm2 = (note+","+note+","+note).split(",");
should for legibility be written, in News, as
var itm2 = (note + "," + note + "," + note).split(",");

Javascript should only be used to write invariant parts of HTML if those
parts are both smallish and surrounded by parts which must be computed.

It's a good idea to read the newsgroup and its FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top