473,394 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Doing Calculations with arrays

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

Sep 12 '06 #1
4 1239
"outstretchedarm" <ou*************@hotmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...

[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[i] = itm1[i];
html += "<option value='" + i + "'>" + arr1[i] + "</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:
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

Sep 12 '06 #2
VK
outstretchedarm wrote:
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][i];

// 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>

Sep 12 '06 #3
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?

Sep 12 '06 #4
JRS: In article <w-******************************@comcast.com>, dated
Tue, 12 Sep 2006 15:19:14 remote, seen in news:comp.lang.javascript,
McKirahan <Ne**@McKirahan.composted :
>
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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: M.Siler | last post by:
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT> <!-- var factor_val = new Array(8,7) factor_val = 68.8 factor_val = 55
9
by: Till Crueger | last post by:
Hi, I have to implement some simple sorting algorithm. I am NOT asking for you to do my homework, but my question is rather on how to store the integers. I recall reading once in here that there...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
4
by: Morten Wennevik | last post by:
Hi, Upon clicking a button I perform some heavy server side calculations lasting up to five minutes. During this time, the web page appear to be loading the response page. Is there some...
2
by: chudson007 | last post by:
This is a very general quastion..... I regularily take data extracts from a SQL server database drop them in excel and run a macro on the data. The macro does nothing more complicated than...
15
by: giff | last post by:
Hi all, I have a doubt, I'll try to expose it to you as clearly as I can, maybe it is not completely in topic, sorry about that. I have a few vectors of doubles (output of some calculations)...
1
by: fsamu2001 | last post by:
how to use radiobutton and checkbox to calculations
1
by: BUmed | last post by:
Normally my forms are simple, but not this one. I need to do some calculations in a form and I would normally do them in query but this is more complicated. I have a form that representation of a...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.