11. Functions Recap - the error message is lying to me


#1

https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/3?curriculum_id=506324b3a7dffd00020bf661

I'm getting the following in the log.
"Hi, I am batman"

I'm getting the following error:
Oops, try again. It looks like you didn't print anything to the console! Did you remember to console.log() the result of passing your name to the nameString() function?

1) So, because I printed what they wanted ("Hi, I am (name)") I'm not printing anything to the console?

2) Why is the original code printing quotes in the console log?


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

If I change the code to the following I will lose the quotes
var nameString = function (name) {
	return "Hi, I am" + " " + name;
};
console.log(nameString("batman"));

With the above code I pass the exercise and there are no quotes.  I don't understand why there are no quotes and I only changed it because I saw someone else with this general format for the code.  I don't feel like I've learned this lesson yet.


#2

The output to the screen is not in quotes. Only string expressions can be sent to the display. To actually print quotes they need to be printable characters in the string.

console.log(nameString('"Batman"'));

To qualify the above statement, think of console.log() as casting output to string in the background.

console.log(Math.PI);

Math.PI will be cast to a string.


#3

Hey, I'm sorry, man. I didn't follow any of that.

You say the output to the screen is not in quotes.

In the first bit of code there are quotes. This is what the question was about. In the second bit of code there were no quotes. Both sets of code functionally have quotes in the exact same places as far as I can tell. Both have quotes after return, and both have quotes around the string we're using as the name.

I'm sure the issue was that my original description wasn't clear enough. I'll try again!
** My first set of code gave output with quotes.
** I modified it very slightly and the quotes vanished from the output, but the code itself still has quotes around the same portions. I'm not understanding why the first attempt had quotes around it.

The last portion of your reply didn't help. It's my fault, I'm sure. It was designed to elaborate on the first part of your response but it made less sense to me. So, it didn't leave me with a greater understanding.


#4
console.log(nameString);

This will output something like, '[Function]'. Not the desired result, no doubt. But, since you have a return statement before it, it is unreachable so doesn't run.

The output you are seeing is the console echoing the contents of its buffer:

=> 'Hi, I am batman'

It is not so evident in the lesson editor console since there is no color highlighting. It is plain white text like a normal console.log() output. Your code did not log this. The console did. That's why there are quotes.


#5

Thanks!

I have no idea what "echoing in the contents of its buffer" means. I mean, it sounds a little like you're messing with me (kidding). But, at the end you describe a process where there's more going on, and the mild change in code order means that what is being displayed is not 100% of the information available which would have given me more to work with.

I really, really appreciate your help with all of this. You have helped me with other questions previously.

It's gotta be frustrating for you to be trying to explain this stuff to people who a week ago only used a computer for cat videos and kardashian news (a mild exaggeration). Sorry about that. heh


#6

The JavaScript console holds the last command it received in its buffer. In your first example, that command (statement in a program) was a return statement with a return value. The value is stored in the buffer and remains there since the program has terminated. This is what gets echoed when our last command is NOT console.log().

Because the lesson editor console has no command line, we don’t see the console as it actually is. go the labs repl.it. This is the same environment (almost) as the lesson editor, but it also has the command line and full color highlighting.

 > enter a command at this prompt
=> what the console responds (in green)

console.log() will output in white.

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

When we run this, we get this result in the console…

=> 'Hi, I am batman'

When we Enter this statement,

 > console.log(nameString("batman"))
 Hi, I am batman

is displayed. Notice there are no quotes?


#8