Reputation: 11
So I am working on a school project to create a "piano" that will play notes that start at a certain time and return an output that looks like this:
Outputs:
Play A
Play B
Wait 1.5 seconds
Play C
Wait 1.5 seconds
Release A
Release B
Release C
So on and so forth. I am able to sort the start times, but I can't figure out how to write something that will translate to the output I need. I'm not sure how to pull the note when the format is note: "A". I am hoping to write something that will pull the notes that start at 0,1,2,etc in order, tell them to play for a certain amount of time and then release. I'm not really sure how else to ask this.
let piano=[
{note: 'Ab', starts_at: 0},
{note: 'A', starts_at: 5},
{note: 'A#', starts_at: 10},
{note: 'Bb', starts_at: 7},
{note: 'B', starts_at: 4},
{note: 'C', starts_at: 8},
{note: 'C#', starts_at: 13},
{note: 'Db', starts_at: 2},
{note: 'D', starts_at: 0},
{note: 'D#', starts_at: 5},
{note: 'Eb', starts_at: 1},
{note: 'E', starts_at: 11},
{note: 'F', starts_at: 3},
{note: 'F#', starts_at: 2},
{note: 'Gb', starts_at: 9},
{note: 'G', starts_at: 10},
{note: 'G#', starts_at: 1}
];
let my_song=piano.sort((elem_1, elem_2,) =>{
if (elem_1.starts_at == elem_2.starts_at){
return 0;
} else if (elem_1.starts_at >= elem_2.starts_at){
return 1;
}
return -1;
});
console.log(my_song)
Upvotes: 0
Views: 60
Reputation: 3518
I think you can put a reduce after you sort the array to build the string value.
const str = piano.reduce((acc, curr, i, arr)=>{
const prev = arr[i - 1];
if(prev) {
const diff = curr.starts_at - prev.starts_at;
acc += `\nWait ${diff} seconds`;
}
acc += `\nPlay ${curr.note}`;
}, '');
Upvotes: 1