Introduction to functions in JS (return keyword)


#1

hello everyone:

I'm stuck with Return Keyword exercise and can't figure out why I keep getting this error message when I run the script provided in the exercise. PLEASE HELP!!!
Here is my code:


var timesTwo = function(number) {
return number * 2;
};
// Call timesTwo here!
var newNumber = function(timesTwo) {
console.log(newNumber);
};
timesTwo(15);


The error displayed is:
"Oops, try again. It looks like you didn't print out the value of newNumber"


#2

return number * 2; seems to not have been indented

Maybe you should try that?


#3

On the exercise it is indented but still doesn't work for me :disappointed:


#4

HI when you begin the lesson you had that code?

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = 
console.log(newNumber);

you only need to save timesTwo() inside the variable newNumber

var newNumber =  timesTwo("here type your number");

you dont put " " around your number


#5

@systemblaster20945,
@ash.mistry,

Indentation is NOT am issue in Javascript
Indentation is a way to format the code, for a better readability...
http://javascript.crockford.com/code.html <---JavaScript code-convention

For the functionality of the return statement
look at the code-sample....

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#6

thank you all for your contributions. The issue has been resolved with the help of wizmarco and leonhard.wettengmx.n


#8

If I understand you correctly the code would mean:

var myFunction = function(param 1) where the function itself is the value stored and saved in the variable?.

function(param 1) is used as a variable itself within that particular function body?

If so, how do I form the syntax in order to successfully call that variable's value into the new script? So basically:

var timesTwo = function(number) {
return number*2;
}; //End of first function body
timesTwo(2);

var newNumber = function timesTwo() {
console.log(newNumber);
};//how do I call the value of return within the new vaiable newNumber?

Thanks


#9

@boardcoder37461

var timesTwo = function(number) {
    return number*2;
};

If you want to call/execute this timesTwo =function=
and this timesTwo =function= was defined
as having 1 parameter number
you will have to provide 1 argument
in our case a "number VALUE" 2
timesTwo( 2 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your timesTwo =function=
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var newNumber = timesTwo( 2 );
console.log( newNumber );

OR directly

console.log( timesTwo( 2 ) );