Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Javascript indexOf not defined

I'm working on a simple web app that essentially allows a guitarist to convert chords based on the positions of their capo - you don't really have to know what that means, but might help if you do lol. I've made it work for an entry of a single chord, but I want the user to be able to enter multiple chords to transpose, so the goal is for them to enter multiple chords which will be stored in an array. From there, I have a preset array of characters for each possible chord, and I have written a nested loop function to search through the array of possible chords so that they can return the index number of that possible chord.

let chordShapes = ["E", "F", "F# / Gb", "G", "G# / Ab", "A", "A# / Bb",
                     "B", "C", "C# / Db", "D", "D# / Eb"];

For example, the user inputs the chords A and E, which will be entered into an array called inpArr. From there, I want the function to return the index of the matching chords in an array called inputChords, so in this case that new array should be [5,0] after this function, and from there I should be able to finish this up. However, I am getting the error that says indexOf is not defined in the function. Here is the function along with the relevant arrays:

let inputChords = ["A", "E"];

function findNums(inpArr){
    let newArr = [];
    for(let i=0; i<inpArr.length-1; i++){
        for(let j=0; j<chordShapes.length-1; j++){
            if(inpArr[i]==chordShapes[j]){
                newArr[i] = indexOf(chordShapes[j]);
                console.log(`inputNum: ${newArr[i]}`); 
            }
        }
    return newArr;
    }
};

findNums(inputChords);

I see from other answers that the discrepancy might be because indexOf is used for strings and arrays (I know strings are a type of array), but this is definitely both of those. All the other questions I've seen about this seem to be about programs above my expertise so I am not sure how to apply their solutions to my own problem here. Any help would be appreciated :)

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

There appears to be a typographical error, where indexOf should be cordShapes.indexOf. However, fixing this will not resolve problems associated with key signatures that use different incidentals for the same chord, and does require users to accurately use the shift key.

One approach may be to lower case user input and use a lookup object for key signatures. E.G.:

const chordShapes = {
  "e": 0, "f": 1,"f#": 2,"gb": 2,"g": 3, "g#": 4,"ab":4 ,"a": 5, "a#": 6,
  "bb": 6,"b": 7,"c": 8, "c#": 9,"db": 9,"d": 10,"d#": 11,"eb": 11
};

let inputChords = ["A", "E"];

function findNums(inpArr){
   return inpArr.map( signature=> chordShapes[ signature.toLowerCase()] );
}

console.log( findNums(inputChords) );

This is still overly simple - you may want to validate and classify chord names before looking up an index for their tablature. The objective would be to separately determine the chord's semitone offset from E, the chord's variation (i.e major/minor, diminished, augmented, 6th, 7th, 9th etc) or if the chord notation is not supported.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!