Console.log the results of a function - 11/13 - Introduction to Functions in JS


#1

The class is saying this code is right and I can move on to the next lesson but in the display box on the right hand side of the screeen, it is showing

[Function]

as the results of the console.log instead of "Hi I am..."

Here is my code:

var nameString = function (name) {

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

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

Thanks!


#2

Actually, I think I may have figured it out.

I need to use:
console.log(nameString("Heidi"));

Instead of:
nameString("Heidi");
console.log(nameString);

Right?


11/13: Why No Need to Call Function This Time?
Can anyone help me to write the below code? Question about 11/13
#3

By saying this:

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

You're just printing the type of nameString which we already know is a Function!
You should use this instead:

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

or

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

:relaxed:
I hope it works!


#4

Yes, that works! Thank you mamos98!


#5

You're welcome! :blush:


#6

That's really interesting, Mamos98...thanks...BUT then this raises the question of Code Academy's/Leng Lee's (the lesson author's) competency, as

someName;
console.log(someName);

has been used by them as -- and thus implied to actually be -- the (or at least a) way to call a function and print its output to the console....

NOT HELPFUL, that, as it's turned out!!


#7

What I don't understand is why we don't need to call the function to get it to run...I think that's where the initial confusion lies -- and in my case, still does!! :frowning:


#8

Where has this:

someName;
console.log(someName);

been used? I see your point that the wording might imply that these are two seperate operation. btw @mamos98 showed that you can make it two without a problem. Where it is better written as one. But is there really an exercise where this is used in the way you mentioned it?


#9

I tried it and you need to call the function to get it run otherwise you get an error message. The idea of a function with return is that the value mentioned after return becomes the new value of your function call so e.g.

nameString("test")

would have a value of "Hi, I am test". Now it's up to you (the programmer) what to do with this value. If you want to store it in a variable for later use:

var val = nameString("test") ;

or whether you want to use it directly without storing it:

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

#10

Can you please review this code for me:

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

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

I getting error message


#11

Probably just a missing space betten , and I


#12

There's nothing wrong with your code...try http://www.webtoolkitonline.com/javascript-tester.html to test it out -- just be sure to make console.log() an alert() instead to test....


#13

Here is my code

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

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

This appears correct. My question, why do I get an error saying Oops, try again. Hmm, it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie. The instructions say pass your name, which I did. So who is Susie and why is she trying to mess with my coding? No where do I have a Susie in my code, so why is the code looking for Susie? Susie are you out here? What did you do to the code Susie?


#14

now if I write it like this

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

};
(nameString("Dave"));

Susie prints, but still get the same error message. I'm frustrated.


#15

Susie is one of the test cases for this exercise. Don't know if there was an important Susie in the life of the author or if it is just a random name. Anyway I guess your problem is that you have 2 spaces added one after the string and one by + " "+ but one should already be sufficient.


#16

Tenes que usar comillas simples hermano. Estas no " ". Estas si ' '.