vendredi 21 août 2020

Find the index of each letter of a given string, and return the same index if the letter is repeated

I'm trying to solve a fairly simple kata and find myself stuck to a conditional. I'm trying to return the same integer for a string that has the same letter more than once.

For example, for the string hello it should return 0.1.2.2.3.

This is my code so far:

const wordPattern = word => {
  word = word.toLowerCase();
  let index =[]
  
  for(i=0; i<word.length; i++){
    index.includes(i) ? word(i) === index[i] : index.push(i)
  }
    
  return index 
}

Thanks a lot in advance!

Aucun commentaire:

Enregistrer un commentaire