[7kyu]Sum of Odd Cubed Numbers

2017-07-04  本文已影响16人  君肄塵

该算法题来自于 codewars【语言: javascript】,翻译如有误差,敬请谅解~


const cubeOdd = arr => {
      const array = arr.filter(el=>typeof el != 'number');
      let sum = 0;
      array.length ? sum = undefined : arr.filter(el=>el%2).forEach(el => {sum += Math.pow(el,3)});
      return sum;
}
function cubeOdd(arr) {
      var sum = 0;
      for (var i = 0; i <arr.length; i++){
        var cubed = (arr[i]*arr[i]*arr[i]);
        if (isNaN(cubed)) return undefined;
        if(cubed % 2 != 0 )sum += cubed;
      }
      return sum;
}
const cubeOdd = arr => arr.some(x=> +x !== x) ? undefined : arr.filter(x=>x%2!=0).reduce((s,x)=>s+x**3,0);
const undefiner = v => isNaN(v) ? undefined : v;
const cubeOdd = arr => undefiner(arr.reduce((s, v) => s + v*v%2*v*v*v, 0));
const cubeOdd=a=>a.filter(x=>typeof x != "number").length?undefined:a.filter(x=>x%2).map(x=>Math.pow(x,3)).reduce((a,b)=>a+b,0)
上一篇下一篇

猜你喜欢

热点阅读