简体   繁体   中英

JavaScript recursive function using ternary operator

why is it necessary to add return statement before ternary operator in recursive function to return function output?

 // 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. The ternary operator decides if the function need to call itself. So the return statement call the same function.

In the example 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 .

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.

return is never default in ternary operation. return is default in Arrow-function but it not default in normal function deceleration. 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 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. 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,
  • 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 . And because of this simplicity, conditional expressions are often used as the body of arrow function.

But it should be no more surpising that you have to have return here than that you have to have it in

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. It's the same thing in your original.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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