这个想法是让一个和弦附加三个值。这些值将存储在一个数组中,因为相同的音符将用于多个和弦。
e.g。
G major = G,B,D
C major = C,E,G
请注意,字母G用于两个和弦
下面是我想要的想法,但我不知道应该使用什么技术。警报仅返回一个值而不是全部三个值。
var notes = new Array();
notes[0] = "A" ;
notes[1] = "B" ;
notes[2] = "C" ;
notes[3] = "C#" ;
notes[4] = "D" ;
notes[5] = "E" ;
notes[6] = "F#" ;
notes[7] = "G" ;
notes[8] = "G#" ;
var Gmajor = notes[7, 1, 4];
var Cmajor = notes[2, 5, 7];
alert(Gmajor);
答案 0 :(得分:1)
您必须为每个多个和弦创建一个新数组:
var Gmajor = [ notes[7], notes[1], notes[4] ];
答案 1 :(得分:1)
notes[7, 1, 4]
与notes[4]
完全相同,如果您对此感兴趣,请阅读逗号运算符
您正在寻找的是:
var notes = [ // changed your initialization to use an array literal instead
"A", // 0
"B", // 1
"C", // 2
"C#", // 3
"D", // 4
"E", // 5
"F#", // 6
"G", // 7
"G#" // 8
];
var Gmajor = [notes[7], notes[1], notes[4]];
var Cmajor = [notes[2], notes[5], notes[7]];
如果您希望将其表示为字符串,则可以执行以下操作:
var GmajorAsString = Gmajor.join(' '); // if you need the array
var GmajorString = notes[7] + ' ' + notes[1] + ' ' + notes[4]; // just string
答案 2 :(得分:1)
您可以创建功能:
var notes = new Array();
notes[0] = "A" ;
notes[1] = "B" ;
notes[2] = "C" ;
notes[3] = "C#" ;
notes[4] = "D" ;
notes[5] = "E" ;
notes[6] = "F#" ;
notes[7] = "G" ;
notes[8] = "G#" ;
var getNotes = function(first, second, third){
return notes[first] + ' ' + notes[second] + ' ' + notes[third];
}
alert(getNotes(7, 1, 4)); // G B D