Q 11 functions recap. Hi, can someone take a look at this? Thanks


#1


#2

You use 2 spaces instead of 1. Also the {} around console.log and the lonely nameString("Tom") are unnecessary. You already have that call in a console.log.


#3

I too get same error, Can you help :slightly_smiling:


#4

Ok the first problem is that,in the last line, you use name instead of an actual name. What I mean is that name is the name of a variable that is never used before or after and will therefore cause an Reference Error. Instead better use a string like "rubysolver02612".

And the other problem is that you probably didn't understand yet how parameters work, do you?
The idea is that the variable in the () after function, called parameter, gets it's value from the value in the () after the function name, called argument.

So if you write
nameString("Naredla")

what happens inside your function is something like this:

var name = "Naredla"

but this is done by the interpreter on its own, you don't have to write it yourself. The problem is that now you'll always replace name with "Naredla" so when the test function runs nameString("Susie") it expects "Hi, I am Susie" but what it gets is "Hi, I am Naredla" because you reassigned it. Hope it's a bit clearer now.


#5

Hi haxor,

First of all, thank you very much for explaining it so well in the above two posts. The only thing that I am not understanding is what it means when it says "...and use console.log to print the output." I know how to use console.log to print but what does it mean to print "output" here?

Thank you,
Safura


#6

Ok, this questions is tricky. Not because I don't know the answer but because I don't know where to start without making this tl;dr and also I can't assume your knowledge to see if tl;dr is good or bad :slightly_smiling:

But let's try it and please respond if it is too long or short. The problem here is that the word output is ambiguous. There is this visible human/computer interface known as console and there is this main scope/function scope interface. The problem is that both go via functions and both could be called output.

Let's start with the main scope/function scope interface. Well functions are always some kind of sub program. They are not run on their own, but when you give the start command via the function call, the execution of the main code is interrupted and the function is executed. Also you can not only start the program by the function call but you could as well give information to the function by using parameters.
The other way from the function to the main scope is done by return. First of all it let's you exit the function (if it is not used explicitly the interpreter adds it automatically at the last line) but it also let's you transport a value from the function to the calling scope. So if you would consider parameters an input this would be the corresponding output. Where the function call now has the value that is mentioned after return.

That is the output that is meant here. The other human/computer interface output is different. Here the output is visible to the user but unlike in the first example it is lost for the program. For example

console.log("test") prints "test" but console.log(console.log("test")) prints test and undefined showing that console.log itself has no value.

So long story short that just means to use:

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

get the output from the function and use it as in input for the function that outputs to the user. :wink:
Hope this makes at least a little sense.


#7

not notice in the "Ativar o windows" in the lower right corner


#8

Seems like you wrote "i am" instead of "I am".


#9

Hi,

My name is Jose

Here is the error I get:

I am not able to determine the root cause..


#10

space between the comma and the I am part

"Hi, I am" + " " +name


#11

Thanks!It worked.Never realized that spacing is the culprit..


#12

Hello haxor,

Thank you for writing back, first of all I want to assure you it wasn't tl;dr :slightly_smiling: !

I read the whole thing few times for it to make sense to me. Scope/function scope interface part is still confusing to me :confused: I don't know what that means. But other than that I think I understood everything.

Thank you,
Safura


#13

I think it is much easier to just create a new variable. If you do that, you can put the console.log anywhere you like.

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


#14

@liahkim0
I'm pretty sure this will create an Reference error for the unknown variable newName. Maybe if you run it twice you keep the variable in the memory still this is really bad habit and no user will run your side twice to make it work.

@safura01
Well if you write for example:

var name;

then you can use name after this statement to refer to a particular place in the memory to store a value (name = value) or to retrieve a value (name). And the scope means the region where this is possible (I hope the linked wiki article is not contradicting :slightly_smiling: ). If you just write statements that doesn't really matter as you stay in the main scope and you wouldn't even need to name it like this as it is the only scope available.

The interesting stuff happens when you introduce functions because functions have their own scope in the {}. So if you write:

var name = function(para){
    var test;
};

then name is inside the main scope and any variable that was declared before name is available inside of the function (global variables). But for example the parameter para or the variable test are declared inside of the function so they are only available inside of this function (local variables). If you'd try to use them outside of the function you'd be told that there is no such variable. So the region where test is defined is the scope of the function name (function scope) and the scope outside of the function is called as said main scope.

Now as said before you could think of functions as programs inside of programs. So apart from the environment (global variables) you could treat functions as black boxes (no knowledge of the process just input output informations). For example console.log, you don't know how the stuff is printed to the console, you just know that the arguments you add in the () of console.log are displayed to the screen. So there are mainly two ways you can interact with the function (interface). Input: You can pass values to the function by using parameters and arguments and output: you can use return to pass information from the function to the calling scope.

I hope this makes it a little clearer.


#15

thank you haxor789, worked


#16

This worked for me:


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