Problems with 11. Function Recap. Help?


#1

Hey, I am having trouble (again) and it would be awesome if you could help. It is very strange, the code is:

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

and in the console box it says:

Hi, I am Susie
Hi, I am Susie

and the problem apparently is:

Oops, try again. Hmm, it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie.

But the thing is, is that it does, just twice. I've had this problem before, and i recieved help, but still dont understand why or what I'm doing to get this. Any ideas?


#2

The important point in the error message is this:

Oops, try again. Hmm, it looks like your nameString() function doesn't return 'Hi, I am Susie' for the name Susie.

Well what does it return for the name Susie? Well let's find out test:

console.log(nameString("Susie"))

What you'll see is undefined (in addition to the other outputs) which is not the string "Hi, I am Susie" and therefore you get the error message.

The problem is the following return interacts with the program in the way that it a) goes back to the function call when it is reached and b) replaces the function call with the value after it. So this makes it possible to pass a value from the function to the caller. Console.log on the other hand just prints something to the console but after this the value is gone if it isn't stored else where. So if the test function expects to get a response from the function printing it doesn't help. That is also the reason for the second message, 1 is generated by yourself and the other one comes from the test function running your code. So better just return the string and console.log the returned value.


#3

Ok, thank you! I think I get what is making it do that, but how do I fix it?


#4

As said just get rid of the console.log inside of the function (just return the string) and wrap the function call (nameString("Susie")) in a console.log.


#5

Thank you! Your'e genius:)


#6

A post was split to a new topic: Problems with 11. Function Recap