2020-05-27 5 kyu How Many Number

2020-05-27  本文已影响0人  苦庭

https://www.codewars.com/kata/55f5efd21ad2b48895000040

My answer / AC

function maxSumDig(nmax, maxsm) {
  var res = [];
  var isSatisfy;
  for(var i=1000; i<=nmax; i++) {
    let arr = i.toString().split("");
    isSatisfy = true;
    for(var j=0; j<=arr.length-4; j++){
     if(Number(arr[j]) + Number(arr[j+1]) + Number(arr[j+2]) + Number(arr[j+3]) > maxsm) isSatisfy = false;
    }
    if(isSatisfy) res.push(i);
  }
  var sum = res.reduce((a,b)=>Number(a)+Number(b));
  var mean = sum/res.length;
  var meanmap = res.map(a=>Math.abs(Number(a)-Number(mean)));
  var index = meanmap.indexOf(Math.min(...meanmap));
  return [res.length, res[index], sum];
}

Recap

上一篇下一篇

猜你喜欢

热点阅读