1

I'm just writing a very simple function in javascript to calculate a factorial. I understand that in javascript you can assign a function to a variable.

So I have tried this on an online compiler (https://repl.it/languages/javascript) and this is what my code looks like

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  };
 console.log("the factorial of "+mynum+" is "+answer);
}

But when I run this the output keeps including the whole function as "answer"

You entered 23
the factorial of 23 is function (mynum) {var _loopStart = Date.now(),_loopIt =     0;
var i = mynum - 1;
var temp = mynum;setTimeout(function () {_loopStart = Infinity;});
while (i > 0) {if (++_loopIt > 5000 && Date.now() - _loopStart > 150) throw new RangeError("Potential infinite loop. You can disable this from settings.");
  temp = temp * i;
  i--;
}
return temp;

}

However i don't have this issue when i create the function and then call it separately (something like answer = function(mynum).

Can anyone please let me know why this is happening?

Thanks!

Steven
  • 23
  • 2
  • 2
    You have to call *answer* and pass it a value, so: : `...+answer(mynum))`. – RobG Mar 21 '17 at 22:51
  • Think about this - Your function takes a parameter `function (mynum)`. When you call it with `the factorial of "+mynum+" is "+answer` what parameter are you expecting it to use? – takendarkk Mar 21 '17 at 22:52

4 Answers4

1

Assigning a function to a variable is different from assigning its evaluation.

In your case, you have two solutions available :

Make an effective call to your assigned function at logging time:

console.log("the factorial of "+mynum+" is "+answer(mynum));

Make an effective call to your assigned function at assignation time:

answer = (function (mynum){
  var i = mynum-1;
  var temp = mynum;
  while(i > 0) {
    temp = temp*i;
    i--;
  }
  return temp;
}(mynum));

Both solutions are quite equivalent for your specific situation.

Why?

Because declaring a function like so:

var func = function () {
  console.log("Hello!");
};

Or like so:

function func () {
  console.log("Hello!");
};

Has little difference

Community
  • 1
  • 1
Telokis
  • 3,399
  • 14
  • 36
  • @Ninetainedo, at the end of the function called at assignment time you added `(mynum)`. Why? What is that called, or otherwise where is a good resource which explains this? – Adam Patterson Mar 21 '17 at 23:09
  • As you can see on the first line of the code block, the function is expecting exactly one parameter (poorly named `mynum` like the variable above). So I have to pass an argument to the function when I call it. It's the exact same thing that happens on the code block above. – Telokis Mar 21 '17 at 23:12
  • 1
    @AdamPatterson He added it so that the function will run and return a value. It's called an IIFE, and you can read about them here: http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript – dlsso Mar 21 '17 at 23:16
1

As pointed out, you have to call it as a function.

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  };
 console.log("the factorial of "+mynum+" is "+answer (mynum));
}

Alternatively, you could use IIEF(mmediately invoked function expression):

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = (function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  })(mynum);
 console.log("the factorial of "+mynum+" is "+answer);
}

Note that I've added a parenthesis around your function and passed in arguments. That's how you can immediately invoke functions.

Sinisag
  • 1,348
  • 12
  • 12
0

When you return a function in javascript such as answer(). You must call it as such.

console.log("the factorial of "+mynum+" is "+answer(mynum));

Callat
  • 2,928
  • 5
  • 30
  • 47
  • so from what i am understanding, doing something like var = function(x) is not calculating function(x) and assigning the return value to var, but rather making var a function itself? Does that mean there is no difference between whether i do var = function(x) or var = function(y) or even var=function()? – Steven Mar 21 '17 at 22:55
  • Alternatively, you can use IIEF(immediately invoked function expression). – Sinisag Mar 21 '17 at 22:58
  • @Steven from my experience with javascript if you pass it without `()` at the end then it sets to a function itself. If you pass it with `()` it recognizes it as a value-returning function and stores the returned value in the variable. – Callat Mar 22 '17 at 01:03
0

You need to use the function that you wrote by calling it i.e

function add (a,b) {
  return a + b;
}

add(1,2);
ukaric
  • 428
  • 1
  • 7
  • 22