在for循环内部的return语句返回初始值。

huangapple go评论69阅读模式
英文:

Return inside for loop return initial value

问题

在FOR循环中的RETURN语句总是返回初始的fact值(即fact = 1)是因为它在循环的第一次迭代中就被执行了。如果你希望返回阶乘的最终结果,你应该把RETURN语句移动到FOR循环之外,以便在循环结束后返回最终的fact值。以下是修改后的代码:

const factorial = function(a) {
  let fact = 1;
  if (a === 0) {
    return 1;
  } else {
    for (let i = 1; i <= a; i++) {
      fact *= i;
    }
    return fact;  // 把RETURN移到FOR循环之外
  }
};

这样修改后,函数将正确返回输入值的阶乘而不是初始的fact值。

英文:

I have a problem to understand why does RETURN in FOR loop returns me always initial fact value (let fact = 1):

const factorial = function(a) {
  let fact=1;
  if(a===0){
    return 1;
  }
  else{
    for(let i=1; i&lt;=a; i++){
      fact*=i;
      return fact;**   // This return
    }
    return fact  
  }
  return fact;  
};


//Result for all values e.g 1,5,10 is always 1, as in initial let fact =1

And when I remove it, my factorial function works totally fine. I have no idea why does it happen:

const factorial = function(a) {
  let fact=1;
  if(a===0){
    return 1;
  }
  else{
    for(let i=1; i&lt;=a; i++){
      fact*=i;
      //Nothing
      }
    return fact  
  }
  return fact;  
};
// Code works fine, I receive factorial correctly

I was expecting to return factorial of my initial value. Instead I receive the initial variable value(1).

答案1

得分: 4

如果我理解你的问题正确,你想知道为什么在第一个代码中,无论输入值是什么,都会得到返回值 1。这是因为你在 for 循环内部有一个 return 语句,这意味着你的 for 循环只会执行一次,然后返回值为1。实际上,for 循环没有迭代,这意味着你永远无法计算阶乘的值,即 fact = 1 * 2 * 3 * ...,你只执行了初始步骤,即 fact = 1

const factorial = function(a) { // &lt;- 你已经测试了这个函数,分别用1、5和10作为参数
  let fact=1;                   // &lt;- 这里将 fact 变量的值设置为1
  if(a===0){                    // &lt;- 你检查传入的值是否等于1,但你使用了1、5和10,因此这里不会返回值1
    return 1;                  //     因为你的条件中使用了1、5和10,所以这里不会返回值1
  }
  else{                        // &lt;- 当 a 的值不等于0 时,进入此代码块
    for(let i=1; i&lt;=a; i++){   // &lt;- 将 i 的值设置为1,并进入 for 循环
      fact*=i;                 // &lt;- 将 fact 的值,初始为1,与 i 的值相乘,i 也是1,因此 fact=1
      return fact;             // &lt;- 在这里返回 fact 的值,即1
    }
    return fact  
  }
  return fact;  
};
英文:

If I've understood your question correctly, you are wondering why did you get a return value of 1 in the first code regardless of the input value. That is because you had a return statement inside your for loop, which meant that your for loop would only be entered once and then exited with a return value of 1. Essentially, for loop never iterates, which means that you never get to calculate the value of factorial as fact = 1 * 2 * 3 * ..., you only do the initial step, which is fact = 1.

const factorial = function(a) { // &lt;- you have tested this with 1,5 and 10
  let fact=1;                   // &lt;- here you set fact variable to value of 1
  if(a===0){                    // &lt;- you check if you sent value is equal to 1,
    return 1;                  //     since you have used 1, 5 and 10 you will not be returning a value of 1 here
  }
  else{                        // &lt;- you enter this block of code when the value of a≠0
    for(let i=1; i&lt;=a; i++){   // &lt;- you set the value of i to 1 and you enter the for loop
      fact*=i;                 // &lt;- you multiple your fact value, which is 1, with a value of i, which is also 1, which means you will get fact=1
      return fact;             // &lt;- you return a value of fact, which is 1 here!
    }
    return fact  
  }
  return fact;  
};

答案2

得分: 2

return 操作符不仅会中断 for 循环,还会停止整个函数的执行1

因此,当您在 for 循环内部没有任何条件地使用 return 时,该返回语句会在该循环的第一次迭代(fact 为1时)中停止整个函数的执行。

这意味着,您的代码:

const factorial = function(a) {
  let fact=1;
  if(a===0){
    return 1;
  }
  else{
    for(let i=1; i<=a; i++){
      fact*=i;
      return fact; // 这个 return
    }
    return fact  
  }
  return fact;  
};

实际上等同于:

const factorial = function(a) {
  let fact=1;

  if (a === 0){
    return 1;
  } else {
    let i = 1;
    fact *= i;
    return fact;
  }
};

for 循环内部使用 return 操作符的有效条件示例:

function isAnyStringInside(values) {
  for (let i = 0; i < values.length; i += 1) {
    if (typeof values[i] === 'string') {
      return true;
    }
  }

  return false;
}

isAnyStringInside([1,2,3,4,5,'Hello',2,3,4,5]); // true
isAnyStringInside([1,2,3,4,5,2,3,4,5]); // false
英文:

return operator does not breaking only for loop: it goes deeper and stops whole function.

So, when you left return inside for without any condition, the return stops whole function at the first iteration of that loop (when fact is 1).

That means, that your code:

const factorial = function(a) {
  let fact=1;
  if(a===0){
    return 1;
  }
  else{
    for(let i=1; i&lt;=a; i++){
      fact*=i;
      return fact; // This return
    }
    return fact  
  }
  return fact;  
};

In fact, is equal to:

const factorial = function(a) {
  let fact=1;

  if (a === 0){
    return 1;
  } else {
    let i = 1;
    fact *= i;
    return fact;
  }
};

Example of valid condition for return operator inside for loop:

function isAnyStringInside(values) {
  for (let i = 0; i &lt; values.length; i += 1) {
    if (typeof values[i] === &#39;string&#39;) {
      return true;
    }
  }

  return false;
}

isAnyStringInside([1,2,3,4,5,&#39;Hello&#39;,2,3,4,5]); // true
isAnyStringInside([1,2,3,4,5,2,3,4,5]); // false

huangapple
  • 本文由 发表于 2023年2月7日 03:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定