这个想法是有一个和弦,有三个价值附加在它。这些值将存储在数组中,因为相同的音符将用于多个和弦。
例如:
G大调= G,B,D
C大调=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);发布于 2013-11-13 12:47:24
您必须为每个多个和弦创建一个新的数组:
var Gmajor = [ notes[7], notes[1], notes[4] ];发布于 2013-11-13 12:47:58
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发布于 2013-11-13 13:02:42
您可以创建函数:
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 Dhttps://stackoverflow.com/questions/19954071
复制相似问题