Explain, please, why the code does not work. He gives out NaN:

function sum (a, b) { var c = this.a + this.b; return console.log(c); } sum(3+3); 

I tried and so, it still does not work:

 function sum (a, b) { var c = a + b; return console.log(c); } sum(3+3); 

What I can not understand?

Closed due to the fact that off-topic participants Kromster , 0xdb , Edward , Kosta B. , AivanF. 21 Jul '18 at 9:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Kromster, 0xdb, Edward, Kosta B., AivanF.
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Thanks, error removed. This is how it worked: function sum1 (a, b) { var c = a + b; return c; } console.log(sum1(3, 3)); function sum1 (a, b) { var c = a + b; return c; } console.log(sum1(3, 3)); - spectre_it

2 answers 2

Use the second option:

 function sum (a, b) { var c = a + b; console.log(c); return c; } 

And call him like this:

sum(3, 3);

Why? Because your method has 2 parameters: a, b .
And you call it with one parameter: sum(3+3); . 3 + 3 is one parameter, before passing it to the function, 3 + 3 will be added, and the function will be called in this form: sum(6, null) .

    You have an error in the transfer parameter. Correct for:

     Sum(3,3)