-
-
Save sethschori/e67ee8034be85a231fd6452510849406 to your computer and use it in GitHub Desktop.
https://repl.it/C1XH/183 created by sethopia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Native Browser JavaScript | |
>>> [ 'F', 'F#' ] | |
[ 'C' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment