Home > Blockchain >  Javascript indexOf not defined
Javascript indexOf not defined

Time:07-16

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 :)

CodePudding user response:

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) );

CodePudding user response:

The code has 3 issues

  1. you should use
    newArr[i] = chordShapes.indexOf(chordShapes[j]);
    instead of
    newArr[i] = indexOf(chordShapes[j]);
    
    
  2. Th return newArr should be outside the second loop
  3. For loops should be < chordShapes.length without -1

let inputChords = ["A", "E"];
let chordShapes = ["E", "F", "F# / Gb", "G", "G# / Ab", "A", "A# / Bb",
                     "B", "C", "C# / Db", "D", "D# / Eb"];
function findNums(inpArr){
    let newArr = [];
    for(let i=0; i<inpArr.length; i  ){
        for(let j=0; j<chordShapes.length; j  ){
            if(inpArr[i]==chordShapes[j]){
                newArr[i] = chordShapes.indexOf(chordShapes[j]);
                console.log(`inputNum: ${newArr[i]} ${chordShapes[j]}`); 
            }
        }
  
    }
      return newArr;
};

findNums(inputChords);

Short Syntax

console.log(inputChords.map(chord=>chordShapes.indexOf(chord)));
  • Related