繁体   English   中英

如何提高函数的性能和可读性?

[英]How can I improve the performance and the readability of my function?

我最近不得不为求职面试做一个测试,提示是编写一个函数,在给定一组负数和正数的情况下,该函数返回最接近 0 的数字。

我的函数运行正常,但我从招聘人员那里听到说我的函数性能不是很好,可读性也不是很好,他们提到了以下问题:

  • 性能 à 双重检查(过滤器 + 排序 + for + 推送 + 保留 + 移位,对 Math.abs 的 4 次相同调用)
  • 缺乏可读性(4 if / else if)

我想知道如何改进我的函数以使其更具性能和可读性?

这是我的功能:

const closestToZero = (arr) => {
  // 1-a variables:
  let positiveNumbers = [];
  let negativeNumbers = [];

  // 1-b returns 0 if the input is either undefined or an empty array
  if(typeof(arr) === 'undefined' || arr.length === 0) {
    return 0
  }

  // 2- filter out non-number values then sort the array
  const onlyNumbers = arr.filter(item => typeof(item) === 'number').sort((a,b) => a-b);

  // 3- split the numbers into positive numbers and negative numbers
  for(let i = 0; i < onlyNumbers.length; i++) {
    if(onlyNumbers[i] > 0) {
      positiveNumbers.push(onlyNumbers[i])
    } else if (onlyNumbers[i] < 0) {
      negativeNumbers.push(onlyNumbers[i])
    }
  }

  // 4- reverse the negative array to get the closest to 0 at the first index
  let reversedNegativeArray = negativeNumbers.reverse()

  // 5- get rid of all the other values and keep only the closest to 0, if array empty return 0
  let closestPositiveNumber = positiveNumbers.length > 0 ? positiveNumbers.shift() : 0
  let closestNegativeNumber = reversedNegativeArray.length > 0 ? reversedNegativeArray.shift() : 0

  // 6- Compare the result of the substraction of the closestPositiveNumber and the closestNegativeNumber to find the closestToZero
  if(closestPositiveNumber - Math.abs(closestNegativeNumber) > 0 && closestNegativeNumber === 0 ) {
    return closestPositiveNumber
  } else if (closestPositiveNumber - Math.abs(closestNegativeNumber) < 0 && closestPositiveNumber === 0) {
    return closestNegativeNumber
  } else if(closestPositiveNumber - Math.abs(closestNegativeNumber) > 0) {
    return closestNegativeNumber
  } else if (closestPositiveNumber - Math.abs(closestNegativeNumber) <= 0) {
    return closestPositiveNumber
  }
}

要求:

  • 如果输入中最接近的数字可以是负数或正数,则函数返回正数
  • 如果输入数组未定义或为空,则函数返回 0

当输入为 [8, 5, 10] 时,函数返回 5

当输入为 [5, 4, -9, 6, -10, -1, 8] 时,函数返回 -1

当输入为 [8, 2, 3, -2] 时,函数返回 2

尽管这不是提供“答案”的最佳 StackExchange 论坛,但 Andreas 确实给出了答案。 一个解决方案可能是这样的:(伪代码)

winner = 0      # arbitrarily assume the zeroth element is closest
for (i from 1 to array-length):
   if abs(array[i]) < abs(array[winner]):    # this one's closer ...
      winner = i

abs() (绝对值)函数当然是“数字与零的接近程度”的指标。 abs(-3) == 3等等。

你的函数确实很复杂。 您可以充分利用这里的Math.abs函数来清理很多东西。 这是我会给出的 js 实现

test = [-1, 5, 4, -9, 6, -10, -1, 8, 1]; 


function closeToZero(arr){

    if(typeof(arr) === 'undefined' || arr.length === 0) {
        return 0;
    }

    ret = test[0]; 

    for(i = 1; i < test.length; i++){
        if(Math.abs(ret) >= Math.abs(test[i]) ){
            if(Math.abs(ret) == Math.abs(test[i])){
               ret = Math.max(ret, test[i]);
            }else{
                ret = test[i];
            }
        }
    }
    return ret; 

}

print(closeToZero(test));

如果需要,您还可以选择检查错误输入

有可能招聘人员会找到一个使用reduce的解决方案也无法阅读。

但要扩展安德烈亚斯的评论。

这是一个示例代码片段,其中包含一个使用 reduce 的函数,用于比较绝对值或符号。

 const closestToZero = (arr) => { if(!arr || arr.length === 0) return 0; return arr.reduce((acc, x) => (Math.abs(x) < Math.abs(acc) || (Math.abs(x) === Math.abs(acc) && x > acc)) ? x : acc); } console.log(closestToZero()); console.log(closestToZero([])); console.log(closestToZero([8,5,10])); console.log(closestToZero([5, 4, -9, 6, -10, -1, 8])); console.log(closestToZero([8, -2, 3, 2]));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM