0

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)
2
  • how long is a key press? Commented Feb 23, 2020 at 21:54
  • Hello I don't know if you understand well but come on what you need is a report of what was done previously well my opinion is the following you should set up a custom log to receive these reports Commented Feb 23, 2020 at 23:35

1 Answer 1

1

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}`;
}, '');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.