Skip to content

Instantly share code, notes, and snippets.

@sethschori
Forked from anonymous/Change Key (1.5).js
Created July 31, 2016 18:31
Show Gist options
  • Save sethschori/e67ee8034be85a231fd6452510849406 to your computer and use it in GitHub Desktop.
Save sethschori/e67ee8034be85a231fd6452510849406 to your computer and use it in GitHub Desktop.
https://repl.it/C1XH/183 created by sethopia
/*
TRANSPOSE MUSIC
In music a keyboard has the following notes
[A, A#, B, C, C#, D, D#, E, F, F#, G, G#]
A # is "sharp" notation. A# or A sharp, is in between A and B
Create a function transpose() that takes an array of notes and a string with the number of steps. The function should return an array with all the notes in the array transposed up that many steps
ex)
transpose(['E', 'F'], '1 step') -> ['F', 'F#']
transpose(['G'], '5 steps') -> ['C']
*/
function transpose(arr, str) {
var steps = parseInt(str.charAt(0));
var returnArr = [];
var keyboard = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
for (var i = 0; i < arr.length; i++) {
var newNote = 0;
if(keyboard.indexOf(arr[i]) + steps > keyboard.length) {
newNote = keyboard.indexOf(arr[i]) + steps - keyboard.length;
} else {
newNote = keyboard.indexOf(arr[i]) + steps;
}
returnArr.push(keyboard[newNote]);
}
return returnArr;
}
console.log(transpose(['E', 'F'], '1 step'));
console.log(transpose(['G'], '5 steps'));
Native Browser JavaScript
>>> [ 'F', 'F#' ]
[ 'C' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment