DEV Community

Discussion on: Daily Challenge #205 - Consonant String Value

Collapse
 
sabbin profile image
Sabin Pandelovitch • Edited

Javascript solution

const values = [...Array(26).keys()]
  .map(i => String.fromCharCode(i + 97))
  .reduce((o, l, i) => ({ ...o, [l]: i + 1 }), {});

const groupValue = group => 
  group.split("").reduce((acc, val) => acc + values[val], 0);

const solve = word =>
  word
    .split(/[aeiou]/)
    .filter(e => e.length)
    .reduce((acc, val) => (groupValue(val) > acc ? groupValue(val) : acc), 0);

const tests = [
  "zodiacs", //26
  "chruschtschov", //80
  "strength", //57
  "catchphrase", //73
  "twelfthstreet", //103
  "mischtschenkoana" //80
];

console.log(tests.map(solve));
Collapse
 
mellen profile image
Matt Ellen • Edited

Nice! Here's my implementation:

const solve = word => word.split(/[uoiea]/)
                          .map(s => s.split('')
                                     .reduce((acc, c) => acc + (c.charCodeAt(0) - 96), 0))
                          .sort((a, b) => b-a)[0];
Collapse
 
sabbin profile image
Sabin Pandelovitch

Very nice, I wasn't aware of the c.charCodeAt(0) - 96 rule, didn't work with chars very much in all these years to be honest.

Thread Thread
 
mellen profile image
Matt Ellen

It only works with lowercase a to z, because UTF-16 represents them as contiguous code points, starting at 97.