Function Recap


#1

The following code keeps on returning "ReferenceError: 'john' is undefined"

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

nameString(john); // I call the funtion here.

Instruction:
01. Write a function called nameString()
02. It should take name as a parameter.
03. The function returns a string equal to "Hi, I am" + " " + name.
04. Call nameString() by passing it your name, and use console.log to print the output.

Could somebody help please?


#2
nameString(john); // I call the funtion here.

here john is the name is a variable name that is not defined yet. Probably you meant a string "john", did you?


#3

True you have called the function correctly but there are two things here,

  1. first the argument John you passed is not a string which mean it is a variable but you have not defined it yet so you must create something like

var john = "Your Nmae";
  1. Since you are returning the final result just calling the method like this

nameString(john);

works but you cannot see the result so you must pass it as argument to console.log() to see it

console.log(nameString(john));

#4

Here, "john" is an input. Can one defines input/s? My understanding was that input varies, hence, it can't be defined. No?


#5

Inputs or by the correct name arguments are values so they cannot be defined, the problem is that you didn't used a value here but a variable name. Now using a variable is ok because it could be the alias for a value as @rydan explained but your variable is new (never used before) so it has no value and that is why the interpreter complains about it. Ok?


#6

haxor789 and rydan, thank you for your help! essentially one must define variable before using it, but string can be passed as is.

This is what I have, but the interpreter still has difficulty understanding it.

Error message:

"Oops, try again. Hmm, it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie."

Code:

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

nameString("john")

So, when I call the function here, every word "name" in the first line are replaced by john?
The value of "nameString" = john? Also, the other "name" inside the curly bracket will be replaced with john?

I am kind of new at this.

Thanks,
net


#7

hi netwhiz47418! I had the same error message about Susie and finaly fixed it like this:
var nameString = function (name) {
var Lily = "my Name";
return ("Hi, I am" + " " + name);
}
console.log(nameString("Lily"))

P.S. My mistake was placing console.log in return section. After deleing it everything worked.


#8

By using function(name) and calling nameString("john") you declare a variable name inside the function your function that has a value of "john". Just like you'd do with var name = "john". The only special thing is that if you call nameString("xyz") it would be var name = "xyz" so it's quite interesting as you can run the same function but feeding it with different values produces different results. As far as I can see this part is ok in your function so don't bother about it that much just play around a bit and get used to how it is done.

The problem in your code is that you return console.log. Better just return the value "Hi, I am" + " " + name and then console.log it:

console.log(nameString("john");

#9

haxor789, thanks for coming back again to help; I think your explanations are beginning to make sense, will keep on practicing. Thanks again. You are doing a great service.

tmcc thanks to you too. The code works now.

-net


#10

I can also be coded like this....

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

};
var newName = nameString("Mike")
console.log(newName);