11. Functions Recap


#1

Hey, interesting trouble. So I wrote this code:

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

};
nameString("Laimis");

and It prints out my name as I want in the console, but codecademy says "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?"

Then I write the other code, which is this:

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

};
nameString("Laimis");
console.log (name);

And I don't get anything printed out in the console, it's just blank, but I do pass the lesson. What is the problem. I have to print out my name and when I do I can't pass it, but when the console is blank I do pass.


11 Functions Recap
#2

You need to call out the function inside console.log, not seperately


#3

Hi,

from here remove console.log(name);

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

};
nameString("Laimis");

And then change nameString("Laimis") to console.log(nameString("Laimis"));

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

};
console.log(nameString("Laimis"));

#4

Weird. Haven't seen this type of syntax for calling the function inside the console.log. Either I can't remember or codecademy didn't teach this. I remember previous lessons where I had to give a value for parameter (for example my name) and then console.log it using function's name. What is the difference? Anyways, Thanks, when I'll be at home I will try this. Maybe you know any other resource to read more about funtion's returns, calls and so on? I feel weak at this.


#5

You can do it also like in 7. Return keyword

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(2);
console.log(newNumber);

So we did something like this before :wink:

Here the Code one the long way and for the case you need the return twice or more times.

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

};
var newString = nameString("Laimis");
console.log(newString);

Here some links:

More about function
And more about functions
More about return
A piece of console.log


#6

So we create a function and ask to return "Hi, I am..." then create a new var called newString and give it a value of function together with a name, for example "Laimis" and when we console.log it we get "Hi, I am..." together with a name. And it took only to call one var, and it's called return twice, right? Thank you a lot.


#7

Yes you can now use var newString where ever you want and how often you want.
My first way was only the really short way if you only want to console.log the return once.

You are welcome


#8

Forgive my ignorance, but we didn't do something like that. So far, we've been calling variables, not "calling commands" with variables inside of them.

Unfortunately, some of the logic steps I've seen CA take between lessons are incredibly large, without description (e.g. "We've been telling you to use var now for half of the lessons, but for no reason why!") and without any sort of everyday use (no, I'm sorry, I don't need to write a piece of code about the fluxuating price of oranges that I may or may not get right, when I can use excel to do the same thing in much less time).


#9

I'm using the exact same syntax and having the exact same problem. Everything works just fine, but CA will not allow a pass.


#10

Hi,

its okay to talk about that.

I mean we do something like that, that we store the return of a function in a new var.

And my way i do because im lazy :smile:

Sorry for my not totally correct anwser, my english is not the best, i only try my best to help here other users.

@lbarbul1
Give us your code here and the error message and i try to help you.


#11

hey my code is like exactly the same as the examples above but they always say: it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie.


#12

Please show us your code.


#13

Hello,

I have tried both ways and both ways return the correct answer but the
the program says that it is not returning the nameString function, I am stuck and do not know what else to do. Help

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

};
var NewString =nameString('Rita')
console.log(NewString)


#14

And the print out is

Hi, I am Rita


#15

Hi,
first i can see that your return has a whitespace to much.

return "Hi, I am_"+"_"+ name; // the underscores shoes you the places where you have whitespaces

#16

something is wrong with this step... I am 100% sure that my code is right and CA doesn't let me pass this one...


#17

Then I wrote like second it work right but don't pass me and says :" Oops, try again. Hmm, it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie."


#18

@ishutka
@demartini_me

You two please show me your actual code.


#19

var nameString = function (name) {

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

};

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


#20

What's wrong here?More then I changed my name to "Susie"))))