Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: string is not a function when Array.findIdex is used

Not sure if I’m doing an error in the code, but when I try to find the index of the letter z that is in the array I’ve split I will get this error:

TypeError: string "z" is not a function
    at Array.findIndex (<anonymous>)

Here is the code:

const chars: string = "abcdefghilmnopqrstuwxyz0123456789@-_+*(),.#";
let outputMatrix: Array<string> = [];
let outputWord: string = "";

// Split input matrix function 
const splitMatrix = async (inputMatrix: string) => {
  let splitted = inputMatrix.split(''); 
  let uppercaseChars = splitted.findIndex('z');
  console.log(uppercaseChars)
  console.log(splitted)
  return splitted;
}

// Generating random word
const createRandomString = (splittedChars: Array<string>, length?: number) => {
  const defaultLength: number = 12;
  const min: number  = 0;
  const max: number = splittedChars.length;
  let num: number;

  if (length) {
    for(let i: number = 0; i < length; i++) {
      num = Math.floor(Math.random() * (max - min) + min);
      outputWord += splittedChars[num];
    }
    return outputWord;
  } else {
    for(let i: number = 0; i < defaultLength; i++) {
      num = Math.floor(Math.random() * (max - min) + min);
      outputWord += splittedChars[num];
    }
  }
  console.log('Generated password', outputWord);
}

const main = () => {
  splitMatrix(chars)
    .then( (outputMatrix) => {
      createRandomString(outputMatrix);
    });
}

main();

What I want to do is to get the index of the z letter and after this extract all the elements that are before the letter including the letter to uppercase them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The error occurs because you are passing a string (‘z’) directly into the findIndex method, which expects a function as its argument. The findIndex method requires a callback function that tests each element of the array to determine if it matches your criteria

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

    const chars: string = "abcdefghilmnopqrstuwxyz0123456789@-_+*(),.#";
    let outputMatrix: Array<string> = [];
    let outputWord: string = "";
    
    // Split input matrix function 
    const splitMatrix = async (inputMatrix: string) => {
      let splitted = inputMatrix.split(''); 
      // Find the index of the letter 'z' correctly using a callback function
      let uppercaseChars = splitted.findIndex((char) => char === 'z');
      
      console.log(uppercaseChars);
      console.log(splitted);
      return splitted;
    }
    
    // Generating random word
    const createRandomString = (splittedChars: Array<string>, length?: number) => {
      const defaultLength: number = 12;
      const min: number  = 0;
      const max: number = splittedChars.length;
      let num: number;
    
      if (length) {
        for(let i: number = 0; i < length; i++) {
          num = Math.floor(Math.random() * (max - min) + min);
          outputWord += splittedChars[num];
        }
        return outputWord;
      } else {
        for(let i: number = 0; i < defaultLength; i++) {
          num = Math.floor(Math.random() * (max - min) + min);
          outputWord += splittedChars[num];
        }
      }
      console.log('Generated password', outputWord);
    }
    
    const main = () => {
      splitMatrix(chars)
        .then((outputMatrix) => {
          createRandomString(outputMatrix);
        });
    }
    
    main();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading