简体   繁体   English

JavaScript 递归 function 使用三元运算符

[英]JavaScript recursive function using ternary operator

why is it necessary to add return statement before ternary operator in recursive function to return function output?为什么递归function要返回function output的三元运算符前要加return语句?

 // This dose not work function rec(n) { n == 1? n: n + rec(n - 1); } // This works as return statement is added before ternary operator function rec(n) { return n == 1? n: n + rec(n - 1); } // This works function rec(n) { if (n == 1) return 1; return n + rec(n - 1); }

 // If you would like to do this in one line then correct solution would be: let rec = n => n == 1? n: n + rec(n - 1); // Now you dont need to add the return keyword before // This works as return statement is added before ternary operator function rec(n) { return n == 1? n: n + rec(n - 1); } // This works function rec(n) { if (n == 1) return 1; return n + rec(n - 1); }

A recursive function is function which calls itself during the execution.递归 function 是 function 在执行期间调用自身。 The ternary operator decides if the function need to call itself.三元运算符决定 function 是否需要调用自身。 So the return statement call the same function.所以return语句调用同样的function。

In the example n == 1? n: n + rec(n - 1);在示例中n == 1? n: n + rec(n - 1); n == 1? n: n + rec(n - 1); if n=1 then the function should return the value of n if not then the function will call itself with new value that is n-1 .如果n=1则 function 应返回n的值,否则 function 将使用新值n-1调用自身。

You need a return because of你需要退货是因为

n + rec(n - 1);

where the rec(n-1) call needs to return a value to be able to calculate n + rec(n - 1) , and that goes for each call to rec() until n reaches 1 when it just returns 1.其中rec(n-1)调用需要返回一个值才能计算n + rec(n - 1) ,并且每次调用rec()直到n达到 1 时才返回 1。

return is never default in ternary operation. return 在三元运算中绝不是默认值。 return is default in Arrow-function but it not default in normal function deceleration. return 在箭头函数中是默认值,但在正常 function 减速中不是默认值。 to return a output from a normal function execution it is always necessary to add return statement, but it is optional in case of Arrow-function.要从正常的 function 执行返回 output,总是需要添加 return 语句,但在箭头函数的情况下它是可选的。

 function x() { 5;} console.log(x()); // Opuput: undefined let y = () => 5; console.log(y()); // Output: 5

A conditional expression (often called a ternary ) is simply an expression.条件表达式(通常称为ternary )只是一个表达式。 It yields a value, but it doesn't do anything with it.它产生一个值,但它不做任何事情。 In fact, unless it has side-effects, it's totally useless unless you either:事实上,除非它有副作用,否则它完全没用,除非你:

  • return it from a function,从 function 返回它,
  • assign its result to a variable, or将其结果分配给变量,或
  • nest it in another expression in which you do one of these things将其嵌套在另一个表达式中,您可以在其中执行其中一项操作

You may be confused by the fact that arrow functions with single-expression bodies return the result of that expression.您可能会对具有单表达式主体的箭头函数返回该表达式的结果这一事实感到困惑。 It's still being returned by the function, even though you don't explicitly use return . function 仍然会返回它,即使您没有明确使用return And because of this simplicity, conditional expressions are often used as the body of arrow function.由于这种简单性,条件表达式经常被用作箭头 function 的主体。

But it should be no more surpising that you have to have return here than that you have to have it in但是,你必须return这里并不比你必须拥有它更令人惊讶

function add (x, y) {
  return x + y;
}

If you took out the return there, the addition will still happen when the function is invoked, but it won't yield any value.如果您在那里取出return ,则在调用 function 时仍会发生添加,但不会产生任何值。 It's the same thing in your original.你的原作也是这样。

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

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