11 Functions recap


#1

I figured out the correct answer by looking at the other questions, but i would like to know what I was doing wrong with this code and why it was giving me the alter ego "Susie" whenever I submitted the code.

My code:

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

nameString("Techies")

#3

What your doing is creating a function called nameString

var nameString = function (name) {
}

And then inside the function you either use console.log or return to return a value that is incorect. It must be some type of error in codecademy but what your doing there is logging "Hi I Am " + name. Whatname is, is the parameter. You declare your parameter by calling the function like you did:

Hope you understood. But please read the link above my post. Thankyou


#4

sorry i did not understand what you have there and i am having the same problem nerdfighter7437 was having


#5

Ok @lightattheend your creating a function which does stuff, called nameString like this:

var namestring = function(){

}

Normally thats old practice as javascript updated thier way and now its this if you want to create a function

function nameString(){

}

Now we put a parameter name whitch becomes defined when you call the function and it is called name. You could use that in the functions. so...

function nameString(name){

}

Now we do stuff in the function, after all thats whats thier use is..

function nameString(name){
console.log("Hi " + " I " + " am " + name)
}

And lastly so we know the name we call the function defining its property

nameString("lightatthend")

Comprehendo?


#6

Ok at first the problem has nothing to do with Susie. What happens is that the author of this exercise built a few test cases and the first is named "Susie" so when nameString("Susie") doesn't give the correct result you get this error message but that does not mean that you need to pick "Susie" as your name in order to pass.

As @amanuel2 already implied the problem is this line:

return console.log("Hi, I am" + " " + name);

The exercise demands to use to return the value from the function and to console.log it to the screen. Unfortunately that cannot be done in one statement. By console.log you somehow consume the value meaning that yes you print it to the screen but then it is gone. Console.log has no value of its own (undefined) so what you return is undefined which is for example not the Hi, I am Susie that you would expect when calling nameString("Susie"). So what you need to do instead is to return the value from the function (get rid of the console.log here) and then use the value of nameString("Techies") for the console.log. So outside of the function just wrap your function call in a console.log console.log(nameString("Techies"))

Hope this makes sense and can explain the problem.


#7

still doing something wrong here

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

};
nameString("LightAtTheEnd")


#8

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

-Look at number 3 in the instructions. That's how you solve this one.


#9

Thanks!!! Thats was the exact answer I was looking for!!!


#10

oh ok thanks a lot that helped


#11

Yes but as the idea of function is introduced as Don't Repeat Yourself it might make more sense to console.log the output than logging the string once and then returning the value.