简体   繁体   English

如何将两个if语句合并为一个?

[英]How can I combine two if-statements into one?

What is the simplest way to combine the following two if-statements into one if-statement? 将以下两个if语句组合为一个if语句的最简单方法是什么?

if(n===1) return 0;
if(n===2) return 1;​

1) If you want to return for all numbers 1)如果要返回所有数字

You could simply say 你可以简单地说

return n-1;

2): If you want just for these two values 2):如果只想使用这两个值

if (n === 1 || n === 2) return n-1;

Or something like bellow 或类似波纹管的东西

[1,2].indexOf(n) //but this could be a tricky way.
if (n === 1 || n === 2) return n-1;

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

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