关于在给定负数的情况下返回函数'undefined'的问题?



我正在尝试编写代码,如果给出了负数,将返回undefined

我被要求使用JavaScript中的函数来计算矩形,三角形和圆圈的区域。我正确地知道了那部分,但是问题也说:" 如果任何参数为负,则该函数应返回未定义。 "

function calculateRectangleArea (length, width) {
  return length * width;
}
function calculateTriangleArea (base, height) {
  return (base * height)/2;
}
function calculateCircleArea (radius) {
  return radius * radius * Math.PI;
}

我可以计算出良好的区域,但是如果有否定的数字要获得undefined,我无法弄清楚该写什么。是:

if (calculateRectangleArea <0) 
   return "undefined";

您需要测试每个参数以确保它不是负面的,在if语句中,如果有的话,请返回undefined-否则,返回普通计算,就像您已经在执行:

function calculateRectangleArea(length, width) {
  if (length < 0 || width < 0) {
    return undefined;
  } else {
    return length * width;
  }
}
function calculateTriangleArea(base, height) {
  if (base < 0 || height < 0) {
    return undefined;
  } else {
    return (base * height) / 2;
  }
}
function calculateCircleArea(radius) {
  if (radius < 0) {
    return undefined;
  } else {
    return radius * radius * Math.PI;
  }
}

另外,由于当没有值返回时,默认情况下返回undefined,您也只能在所有参数都是非负时返回计算,例如:

function calculateRectangleArea(length, width) {
  if (length >= 0 && width >= 0) {
    return length * width;
  }
}

,因为如果参数的任何是未定义的,请使用 [...arguments]

上的 some
function calculateRectangleArea (length, width) {
  if ([...arguments].some(e => e < 0)) return undefined;
  return length * width;
}

为您的所有功能执行此操作。

最简单,最可读的解决方案是检查所有参数是否在计算该区域之前均为正面,否则您将返回undefined

例如(这是使用条件运算符):

function calculateRectangleArea (length, width) {
  return length < 0 || width < 0 ? undefined: length * width;
}
function calculateTriangleArea (base, height) {
  return base < 0 || height < 0 ? undefined : (base * height)/2;
}
function calculateCircleArea (radius) {
  return radius < 0 ? undefined: radius * radius * Math.PI;
}

(注意:对于圆圈,您可以使用指数运算符,在受支持的地方)

由于您已经为所有计算定义了一个共同的模式,因此您可以概括。

基本上,您只有在参数为正时才能执行给定函数的"后卫",否则您将返回undefined。尽管存在论点和数学,但这种检查仍可以应用于您的所有计算。这是可能的,因为JS具有一流的功能。

以下示例是使用差异语法和箭头函数,但也可以使用常规函数来完成:

// Takes a function as argument and returns a function 
// that will execute the function given only if all the arguments
// provided are positive, `undefined` otherwise:
const guard = fn => (...args) => args.some(arg => arg < 0) ? undefined : fn(...args);
const calculateRectangleArea = guard((length, width) => length * width);
const calculateTriangleArea = guard((base, height) => base * height /2);
const calculateCircleArea = guard(radius => radius ** 2 * Math.PI);

您使用 Math.abs()检查整数或小数是负的。代码将函数定义为对象的属性,函数使用.find()创建一个变量,如果传递给该函数的一个参数为负

,则将其设置为undefined

const calculations = {
  calculateRectangleArea (length, width) {
    const n = this[Object.getOwnPropertySymbols(this)[0]](length, width) 
    return !n ? n : length * width
  },
  calculateTriangleArea (base, height) {
    const n = this[Object.getOwnPropertySymbols(this)[0]](base, height) 
    return !n ? n : (base * height)/2
  },
  calculateCircleArea (radius) { 
    const n = this[Object.getOwnPropertySymbols(this)[0]](radius) 
    return !n ? n : radius * radius * Math.PI
  },
  [Symbol('filter')](...props) {
    const n = props.find(n => Math.abs(n) !== n)
    // if `n` is not `undefined` `return` `void 0` else return `true`
    console.log(!n)
    return n !== undefined ? void 0 : !n
  }
}
console.log(
  calculations.calculateRectangleArea(0, 0) // `0`
, calculations.calculateTriangleArea(1, 2) // `1` 
, calculations.calculateCircleArea(-2, 1) // `undefined`
)

我只想说谢谢您的所有帮助!我设法解决了它,但是让这个社区愿意介入并提供帮助确实很有帮助!我希望有一天我会足够好,可以回报青睐。

谢谢!

最新更新