11. Functions recap


#1

Hi, I'm going along my merry way with this programming, and I thought I got stuck on this until I cracked it.
However even though it lets me move on, I have no idea what it did or what I wrote to display the message that it does.

Could someone guide me in the right direction on what or where I wen't wrong or where I can adjust the code for it to work properly?

Thank you :slight_smile:


#2

what are you doing from line 4 and on wards? Is it an attempt to call the function? A function call exist of the function name, plus the argument you want to pass into the function:

nameString("Joey");

that is it. I don't know what you tried to do?


#3

I tried to make the system say "Hi, I am Joey"... But I got this....That's what my question is... WTF AM I doing after line 4? I have no idea... Where the ■■■■ did it pull [Function] out of? I definitely didn't tell it to say that... One thing I asked it to print and it printed me a function?

How should this be set up?


#4

P.S ■■■■ is not a cuss word... H e l l


#5

like i suggest, remove line 4 till the end, and replace it with a proper function call, like i showed in my first answer


#6

Hi @simonasp

You didn't call the function inside console.log and that's why the name didn't print. This is what you should have wrote:

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

That will print out the name of your nameString.


#7

So I got the same exact result as OP here.

Without the extra brackets on lines 4 and 8, my code is identical to his.

So what you are saying here, is that, the correct code would be to define the variable in nameString ("Joey", in OP's case) in the same line of code as telling it to print it to console? (your example console.log(nameString("Joey"));

OP and I both defined the variable, then printed to console in two separate commands. It seems here, that the console is printing out "[Function]" simply because we told it to console.log(nameString), and nameString is only a function? Would the console always output "[Function]" if we told it to output a function name with no detail?


#8

yea, just putting the name of the function will give that it is a function, if you did nameString(), it would give an error, since the function takes one argument, and zero are provided


#9

I found this explanation in the old codecademy forum to be very helpful; https://www.codecademy.com/forum_questions/54d922aa95e378cf2400a85a


#10

Thanks @caseybus that link explained what I was after perfectly! :slight_smile: I get really confused with the context flow of the code not the actual logic behind it. That explanation nailed it! :wink:


#11

Hey @stetim94 I tried your method and it worked! :slight_smile: Is this a good way of working with code? because it did exactly what I wanted it to do.. But the program is mumbling that it doesn't like it because it's not in console.log...
If anything thank you for showing me another way, and I guess my question now is; would this method be durable in a real life scenario? or would console.log still be a better practice way?

This seems like a shorter better way of coding for me personally:

However this does the same thing more or less and looks a bit more polished...

Would these two methods have any major differences of use in a real life scenario?


#12

Well, it is important to realize codecademy by default prints the last thing to the console, wouldn't this not have happened, nothing would show in the console window in your first code.

So yes, these methods have a real life difference. The second method would show something in the console, the first would just run, but show nothing in the console

Depends on what you want to achieve, sometimes you want to call a function, but you don't need the user to see this, then i would use the first method.

Either way, console is used for debug. Who looks in the console window when browsing a webpage? Not your average user


#13

Perfect! Thank you very much :slight_smile: