rewrite an array of letters to numbers using dictionary

examples:

"hello" => 0.1.2.2.3

"javascript" => 0.1.2.1.3.4.5.6.7.8

"hippopotomonstrosesquippedaliophobia" => 0.1.2.2.3.2.3.4.3.5.3.6.7.4.8.3.7.9.7.10.11.1.2.2.9.12.13.14.1.3.2.0.3.15.1.13

my non-working code:


function wordPattern(word) {
  var res = []
  var dic = []
  var count = 0
  var pipa = word.split("")
  for (i=0;i<pipa.length;i++) {
    if (!dic.includes(pipa[i])) {
      dic.push({key:   count, value: pipa[i]});
      count ++
  }
    for (j=0;j<pipa.length;j++) {
      res.push(dic.key[pipa[i]])
    }
    return res.join("."); 
}

Thanks in advance

6 thoughts on “rewrite an array of letters to numbers using dictionary”

Leave a Comment