I have a JS file that creates an object with notes from a music scale when given a key (aka tonic/ note). The code works and does what I want it to do. I need a critique on the way it's written, what could be made better, quality of code — even if it's indentation. please. I've never written code so I have no frame of reference.
I'm leaving the comments in. I know the getKey()
isn't necessary for what's going on, I put it in there to serve another purpose later.
(function () {
console.time("-------------------------------------------------------------------->> mainFunction");
"use strict";
//gets key and returns tonic object
var getKey = function (key) {
var tonic = {};
tonic[key] = 1;
return tonic;
};
//takes a tonic and type and returns the scale objects in an array
var getScale = function (tonic, type) {
//chromatic scale
var chromatic = ['c', 'c#', 'd', 'Eb', 'e', 'f', 'f#', 'g', 'Ab', 'a', 'Bb', 'b'];
//extract key from tonic object
var extractTonic = Object.keys(tonic)[0];
//get position of key in chromatic array
var positionOfTonic = chromatic.indexOf(extractTonic);
//if positionOfTonic is > 0, split the array into two at above position
if (positionOfTonic !== 0) {
var arrayOne = chromatic.splice(positionOfTonic);
var arrayTwo = chromatic.splice(0,(positionOfTonic));
//reset chromatic to set tonic at index 0
chromatic = arrayOne.concat(arrayTwo);
}
switch(type) {
case "major":
var majorArray = chromatic.slice(0);
var removeValFromIndex = [1, 3, 6, 8, 10];
for (var i = removeValFromIndex.length - 1; i >= 0; i--) {
majorArray.splice(removeValFromIndex[i],1);
}
break;
case "minor":
var minorArray = chromatic.slice(0);
var removeValFromIndex = [1, 4, 6, 9, 11];
for (var i = removeValFromIndex.length - 1; i >= 0; i--) {
minorArray.splice(removeValFromIndex[i],1);
}
break;
}
var getScaleObject = function (array) {
//can't create objects with values from array....try with new constructor.
var superTonic = {};
var mediant = {};
var subDominant = {};
var dominant = {};
var subMediant = {};
var leadingTone = {};
superTonic[array[1]] = 2;
mediant[array[2]] = 3;
subDominant[array[3]] = 4;
dominant[array[4]] = 5;
subMediant[array[5]] = 6;
leadingTone[array[6]] = 7;
var majorObject = {tonic, superTonic, mediant, subDominant, dominant, subMediant, leadingTone};
return majorObject;
};
if (type === "major") {
console.time("-------------------------------------------------------------------->> getScaleObject (major)");
var consoleTimeForMajor = getScaleObject(majorArray);
console.timeEnd("-------------------------------------------------------------------->> getScaleObject (major)");
return consoleTimeForMajor;
}
else if (type === "minor") {
console.time("-------------------------------------------------------------------->> getScaleObject (minor)");
var consoleTimeForMinor = getScaleObject(minorArray);
console.timeEnd("-------------------------------------------------------------------->> getScaleObject (minor)");
return consoleTimeForMinor;
}
};
////////////////INPUT////////////////
console.time("-------------------------------------------------------------------->> getKey");
// console.log(getKey('c'));
console.timeEnd("-------------------------------------------------------------------->> getKey");
console.time("-------------------------------------------------------------------->> getScale(getKey)");
console.log(getScale(getKey('a'),'minor'));
console.timeEnd("-------------------------------------------------------------------->> getScale(getKey)");
// console.time("-------------------------------------------------------------------->> getScale");
// console.log(getScale({c:1},'major'));
// console.timeEnd("-------------------------------------------------------------------->> getScale");
}());
console.timeEnd("-------------------------------------------------------------------->> mainFunction");
/*
todo
rename keys to first||tonic, second||superTonic...
build loop and presentation
add other types
use obj.prototype for inheritance in getKey
bugs
capitalizing [chromatic] elements breaks everything...splice not working?
restrictions
cant compare/calculate enharmonic scales - scaleObject created for a given tonic
*/
The output looks like this:
-------------------------------------------------------------------->> getKey: 7ms
-------------------------------------------------------------------->> getScaleObject (minor): 1ms
{ tonic: { a: 1 },
superTonic: { b: 2 },
mediant: { c: 3 },
subDominant: { d: 4 },
dominant: { e: 5 },
subMediant: { f: 6 },
leadingTone: { g: 7 } }
-------------------------------------------------------------------->> getScale(getKey): 8ms
-------------------------------------------------------------------->> mainFunction: 23ms
I kept the console.time stuff in there, I realize it makes the code look messy, I doubt also that they're accurate measurements. I get a somewhat faster execution on Node vs. the browser console but even then the time fluctuates depending on other things windows is running. Please disregard all that.
I tried to initialize my seven note objects inside getScaleObject() as I was creating them but it didn't seem to work:
var superTonic[array[1]] = 2; //didn't work
I broke that process into two steps and then returned a container object.
I haven't investigated this yet but since I'm writing this... if I change the elements inside the 'chromatic' array, the whole thing melts down.