1

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<=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<=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).

MoRe
  • 2,296
  • 2
  • 3
  • 23
kozuba
  • 37
  • 4
  • Please don't use markup to highlight things in your code, `// use comments`. – tadman Feb 06 '23 at 19:11
  • 1
    Do you understand what `return` does? – PM 77-1 Feb 06 '23 at 19:11
  • 1
    `return` *returns* from the *entire function*, not just the `for`. If you want to stop iterating, use `break`. – tadman Feb 06 '23 at 19:11
  • Why does leaving a function leaves a loop as well? – Konrad Feb 06 '23 at 19:24
  • "Why does leaving a function leaves a loop as well?" Because `return` means "We've done everything we need to do inside this function, return the value to the caller" So, basically, because that's how programmers decades ago decided that's how `return` should work – rook218 Feb 06 '23 at 19:36
  • Since you're learning, try to understand why you put a `return` in your `for` loop to begin with. You want the entire `for` loop to execute and the very next statement after the `for` loop is another `return`, so what did you hope to accomplish with the `return` in the `for` loop? It smells to me like you made a beginner mistake, putting bits of code in places without understanding what it does. Which is fine and part of the process, just make sure you learn from it :) – rook218 Feb 06 '23 at 19:42
  • "Why does leaving a function leaves a loop as well?" And to answer this question in a different way, your function has already given you a value. It's done the job you told it to do. It would be like telling two of your employees, "Drive this machine to this McDonalds in Toronto, but if you see a McDonalds along the way then just drop it off there." They've dropped off the machine (value) according to your specification, do you want to pay the gas, mileage, wage, and overnight for them to get all the way to Toronto empty-handed just to turn back again for no reason? – rook218 Feb 06 '23 at 19:44

2 Answers2

4

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) { // <- you have tested this with 1,5 and 10
  let fact=1;                   // <- here you set fact variable to value of 1
  if(a===0){                    // <- 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{                        // <- you enter this block of code when the value of a≠0
    for(let i=1; i<=a; i++){   // <- you set the value of i to 1 and you enter the for loop
      fact*=i;                 // <- 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;             // <- you return a value of fact, which is 1 here!
    }
    return fact  
  }
  return fact;  
};
Aleksa Majkic
  • 632
  • 5
  • 17
2

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<=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 < 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
EMiller
  • 21
  • 1
  • 1
  • 4