Javascript-简单练习

时间:2019-04-24 06:52:22

标签: javascript arrays function math

我有一个任务来编写一个函数getEvenAverage,该函数应该只接受一个参数-数组。此函数应return来自此数组的偶数平均值。如果数组中没有偶数,则该函数应返回null

非常感谢您提供任何反馈意见:-)

function getEvenAverage(tab) {
  {
    if (i % 2 === 0) {
      for (var i = 0; i < tab.length; i++) {
        sum += parseInt(tab[i], 10);
      }
      var avg = sum / tab.length;
    } else
      console.log('null');
  }
}

7 个答案:

答案 0 :(得分:3)

您说您需要退货,所以return。还要在if循环内移动for语句,并修复其他一些语法错误。就像评论中指出的那样,您应该将sum除以偶数个数来得到avg

function getEvenAverage(tab) {
  var sum = 0;
  var evens = 0;
  for (var i = 0; i < tab.length; i++) {
    if (i % 2 === 0) {
      sum += parseInt(tab[i], 10);
      evens++;
    } 
  }
  if (evens == 0) {
    console.log("null");
    return null;
  } else {
    var avg = sum / evens;
    return avg;
  }
}

console.log(getEvenAverage([1, 2, 3]));

答案 1 :(得分:2)

您还可以通过数组简化操作,只需遍历单个数组

const reducer = (acc, val) => {
  let {
    sum,
    count
  } = acc;
  return (val % 2 === 0 ? {
    sum: sum + val,
    count: count + 1
  } : acc);
};

const getEvenAverage = (input) => {
  const initialValue = {
    sum: 0,
    count: 0
  };
  const output = input.reduce(reducer, initialValue);

  if (output.count === 0) {
    return null;
  } else {
    return output.sum / output.count;
  }
};

console.log(getEvenAverage([1, 2, 3]));

答案 2 :(得分:0)

这是正确的功能。

CL-USER 30 > (defvar *temp* '(1 2 3))
*TEMP*

CL-USER 31 > (let ((temp *temp*))
               (eq temp *temp*))
T

祝您编码愉快。

答案 3 :(得分:0)

尝试使用此功能

function getEvenAverage(tab) {
    var sum = 0, count = 0;
    for (var i = 0; i < tab.length; i++) {
      if (i % 2 === 0) {
        sum += parseInt(tab[i], 10);
        count++;
      }
    } 
    if(sum > 0)
      return (sum / count);
    return null;
}

答案 4 :(得分:0)

您只需要偶数,因此首先将数组filter放入一个新数组,然后将所有数字求和(使用reduce或for循环)并除以其长度。

function getEvenAverage(array) {
  if (!Array.isArray(array)) return null; // not a must if you're sure you pass an array
  var evenArray = array.filter(function(value) {
    return value % 2 === 0
  });
  if (evenArray.length === 0) return null;
  var evenSum = evenArray.reduce(function(total, current) {
    return total + current;
  });
  var evenAvg = evenSum / evenArray.length;
  return evenAvg;
}

console.log(getEvenAverage("not an array"));
console.log(getEvenAverage([1,3,7])); // no even numbers
console.log(getEvenAverage([1,2,3])); // single even number
console.log(getEvenAverage([2,2,2])); // only even numbers
console.log(getEvenAverage([1,2,3,10,18])); // bigger array
console.log(getEvenAverage([0,1])); // 0 is also even

答案 5 :(得分:0)

除了使用for循环外,您还可以使用filter和reduce Array方法。

function getEvenAverage(arr) {
  const newArr = arr.filter(number => number % 2 === 0);
  return newArr.length > 0 ? newArr.reduce((acc, num) => acc + num) / newArr.length : null;
}

console.log(getEvenAverage([1, 2, 3, 4]));
console.log(getEvenAverage([1, 3, 5, 7]));

答案 6 :(得分:0)

function getEvenAverage(arr){
  var evenNumbers = []; // we use an array to hold all of our evenNumbers
  for  (var el of arr){ // we loop over the received array to check the received
    if(el % 2 !=0){ // if the number is even
      evenNumbers.push(el); // we add it to our evenNumbers array
    }
  }
  if(evenNumbers.length == 0){ // when we have no even Number
    return false; // we then return false
  }
  else{
    // the next block of code calculates the average of the even values
    return evenNumbers.reduce((pv,cv) => pv+cv,0)/evenNumbers.length;
  }
}

var evenNumbers = [4,2,3,6,5,9];
getEvenAverage(evenNumbers); // returns 5.666666666666667
getEvenAverage([2,4,6,8]); // returns false