Problems with 11. Function Recap


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

Problems with 11. Function Recap. Help?

Running this I get an error concerning that S comes out of nowhere. Could you explain what you intended to do with it?


S comes from inside the function:

return S="Hi, I am" + " " + name;

but that is not how this works


@yowsimmo, you should call your function first, before you try to log something which is inside the function:

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

your function doesn't gets executed until you call it, so then S is still undefined. I am still not sure this is the recommended way.


I am still not sure this is the recommended way.

No it's not. This would use a global variable S to return something to the screen. Not only uses it a rather dangerous form of global variables that implicitly adds a property to the window object:
but also it is simply not necessary as the return already gives the opportunity to get the value back to the main scope e.g.

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

would make far more sense.


I already suspected that, and in this exercise there is no need to store the returned string in a variable, so the other recommended way is:


Thanks haxor, always good to learn from you :slightly_smiling:

@yowsimmo, hopefully you can resolve the issue with our suggestions.


i tried this and output came.

Hi,I'm ahab
Hi,I'm yowsimmo

see this

with no error.


Weird, still, as you can read in our reply's it is bad practice.


What happens is the following, when you press submit your code runs an SCT (submission correctness test) that runs your code with a few inputs to see if it is working and it seems like "Susie" is the first and "Captain Ahab" is the last test to check your code. So as you're doing something unexpected you see code that is meant to only run in the background.


Thanks, I reveiwed the function recap and recorrected the line, It worked and my confussion clear

output came in single line
Hi, I am yow simmo