In the sample code below, it basically re-arranges the order of an array by using the slice method. I’ve chosen the slice method because it’s a non-destructive one. The conventional for
loop I wish I could use a foreach
(doable), but it would be nice to have a way to offset it otherwise I’ll be imperative again buy using if(idx === 0)
do something. Maybe there’s a way to be more functional? Plus there’s usage of let
vars
const major = {
base: ["C", "D", "E", "F", "G", "A", "B"],
flats: [[], [], [], [], [], [], []],
createCircleOf4ths: function () {
let left = this.base.slice(0, 3);
let right = this.base.slice(3);
this.flats[0] = right.concat(left);
this.flats[0][3] = this.flats[0][3] + "b";
for (let i = 1; i < 7; i++) {
left = this.flats[i - 1].slice(0, 3);
right = this.flats[i - 1].slice(3);
this.flats[i] = right.concat(left);
this.flats[i][3] = this.flats[i][3] + "b";
}
},
};
The output would be:
0: (7) ["F", "G", "A", "Bb", "C", "D", "E"]
1: (7) ["Bb", "C", "D", "Eb", "F", "G", "A"]
2: (7) ["Eb", "F", "G", "Ab", "Bb", "C", "D"]
3: (7) ["Ab", "Bb", "C", "Db", "Eb", "F", "G"]
4: (7) ["Db", "Eb", "F", "Gb", "Ab", "Bb", "C"]
5: (7) ["Gb", "Ab", "Bb", "Cb", "Db", "Eb", "F"]
6: (7) ["Cb", "Db", "Eb", "Fb", "Gb", "Ab", "Bb"]
All the useful major scales with flat accidentals.