简体   繁体   English

在 Javascript if-else 语句中包含“或”的多个条件

[英]Multiple criteria with including 'or' in Javascript if-else statement

I'm newbie in Javascript.我是 Javascript 的新手。 Trying to write a function in which I will enter value for three items and it will return the total price.尝试编写 function ,我将在其中输入三个项目的值,它将返回总价。 If I enter negative value for any parameter than it will give an error message instead of total price.如果我为任何参数输入负值,它将给出错误消息而不是总价。

Expected results pseudocode:预期结果伪代码:

if pen < 0 or pencil < 0 or sharpner < 0 then errorMessage else totalPrice如果 pen < 0 或 pencil < 0 或 sharpner < 0 那么 errorMessage 否则 totalPrice

My code is:我的代码是:

function costCalc (pen, pencil, sharpner){

var penPrice = pen * 10;
var pencilPrice = pencil * 20;
var sharpnerPrice = sharpner * 5;

if (penPrice < 0 || pencilPrice < 0 || sharpnerPrice < 0 ){
    return "Something went wrong."
} else{
    var totalPrice = penPrice + pencilPrice + sharpnerPrice;
    return totalPrice
    }
}

I've 'return' twice here.我在这里“返回”了两次。 Is this code ok?这段代码可以吗? Or, I've to change something?或者,我必须改变一些东西?

Advance thanks for your cooperation.提前感谢您的合作。

Regards.问候。

Yes, what you are doing is fine.是的,你正在做的很好。 The moment you return from a function the remaining code is not executed, the function is "finished".从 function 返回的那一刻,其余代码未执行, function 已“完成”。 In fact, it is very common to return from a function and intentionally skip the code after the return statement.实际上,从 function 返回并故意跳过 return 语句后的代码是很常见的。

There is nothing wrong with your code but there are alternative ways of implementing it.您的代码没有任何问题,但有其他实现方式。 A few examples, for your reference, could be:一些示例供您参考,可能是:

/* 
 * `[item]Price` can be < 0 only if `[item]` < 0 so you can anticipate the check
 * in order to avoid unnecessary calculations
 */

function costCalcB(pen, pencil, sharpner) {
  if (pen < 0 || pencil < 0 || sharpner < 0) {
    return "Something went wrong."
  } else {
    const penPrice = pen * 10;
    const pencilPrice = pencil * 20;
    const sharpnerPrice = sharpner * 5;
    const totalPrice = penPrice + pencilPrice + sharpnerPrice;
    return totalPrice;
  }
}

/*
 * You can also avoid the `else` block
 */
 
 function costCalcC(pen, pencil, sharpner) {
  if (pen < 0 || pencil < 0 || sharpner < 0) {
    return "Something went wrong."
  }
  
  const penPrice = pen * 10;
  const pencilPrice = pencil * 20;
  const sharpnerPrice = sharpner * 5;
  const totalPrice = penPrice + pencilPrice + sharpnerPrice;
  return totalPrice;
}

/*
 * In case of error, you can also throw an exception rather than return
 */
 
 function costCalcD(pen, pencil, sharpner) {
  if (pen < 0 || pencil < 0 || sharpner < 0) {
    throw "Something went wrong."
  }
  
  const penPrice = pen * 10;
  const pencilPrice = pencil * 20;
  const sharpnerPrice = sharpner * 5;
  const totalPrice = penPrice + pencilPrice + sharpnerPrice;
  return totalPrice;
}

try {
    costCalcD(10, 10, -1);
} catch(e) {
    console.log(e);
}

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

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