I have written a simple function that checks if the provided note is valid by looping over an array and comparing values.
The code always returns false and I am unsure why?
When I run the code item.split("/")[1] == note in isolation it returns true (with note = "C" ) so why is my function always returning false?
const chromatic = ["A", "A#/Bb", "B/Cb", "B#/C", "C#/Db", "D", "D#/Eb", "E", "E#/Fb", "F", "F#/Gb", "G", "G#/Ab"];
const isValidNote = (note) => {
chromatic.forEach((item) => {
if (item.split("/").length > 1) {
console.log(item.split("/")[1] == note); // logs true on 3rd iteration
if (item.split("/")[0] == note) return true;
if (item.split("/")[1] == note) return true;
} else if (item == note) {
return true;
}
});
return false;
}
console.log(isValidNote("C"));