Question about 11/13


#1

var nameString = function (name) {
return "Hi, I am" + " " + name;
};

nameString("Dave")

console.log(nameString());

Code.academy says "Great!,' but my code returns "Hi, my name is undefined"
What's wrong?


#2

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

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".**


#3

You defined a nameString function which takes 1 parameter name
and this name parameter is used as a variable throughout the FUNCTION-BODY.

At the moment that you call this nameString function
and the nameString function was defined as having 1 parameter name
you will have to provide 1 argument, in this case that would be a string Value 'Dave'.

console.log( nameString('Dave') );


#4

++++ 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 ) );

#5

Your name is coming back undefined because you are using the console.log command to print your name,but the brackets are empty inside,so there is nothing to print making it undefined. You should remove nameString("Dave") and instead write your console.log as : console.log(nameString("Dave"));.


#6

Hi Don7,
you have not mention the name in console.log.

Execute the below code you will get output.

var nameString = function (name) {
	return "Hi, I am" + " " + name;
	};

nameString("Vikram");
console.log(nameString("Vikram"));

#7

That can't be the answer, Vikram...just too redundant, even as a pedagogical tool!!

Anyway, it's clear Code Academy/Leng Lee (the author of these lessons) goofed...my question is, why isn't 11/13 being updated with more helpful information??


#8

I don't understand the point of supplying a function a parameter ("name") only to have to "manually" print out that parameter oneself anyway!

I mean, what's the sense in supplying nameString with name when we gotta spell out the actual value of name ourselves all the same??


#9

Ah, consider mamos98's answer here...http://discuss.codecademy.com/t/console-log-the-results-of-a-function-11-13-introduction-to-functions-in-js/1275/2 -- makes sense!


#10

If you mean redundancy as like using a function to print a sentence that could have been printed just in the same way without the function, then yes your right it simply is redundant. The only redundant part of @vikramjoiser answer is this:

nameString("Vikram"); <-- redundant
console.log(nameString("Vikram"));

it computes a value of "Hi, I am vikram" that is never again used. Probably the intention is to show you functions by doing stuff you already know about. The interesting part about functions is that you can run them infinite times and whenever you like it and the interesting thing about parameters is that you can run the same function but as you change the input you might change the output. So you can use the exact same function to print

Hi, I am Don7
Hi, I am haxor789
Hi, I am Vikram
Hi, I am dc_super_rich

aso so on just by having written this function once. All you need to do is to call it with a different value:

console.log(nameString("Don7"));
console.log(nameString("haxor789"));
console.log(nameString("Vikram"));
console.log(nameString("dc_super_rich"));

And if you count the writing now it might become less redundand, does it?