Codewars.com task "String average"

in codewars •  2 years ago  (edited)
function averageString(str) {
  let num = 'zero,one,two,three,four,five,six,seven,eight,nine'.split(',');
  let arr = str.split(/\s/);
  let all = arr.map(s=>num.indexOf(s));
  let avg = all.reduce((a,b)=>a+b,0)/arr.length;
  let na = 'n/a';
  return all.some(n=>n<0) ? na : num[Math.floor(avg)] || na;
}

http://www.codewars.com/kata/string-average/javascript

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!