I get to pass the exercise but I am not sure if its the right answer


#1



Hello, I have a question about calling a function by passing it your parameter, and use console.log to print the output.
My code is as below:


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

The output is as below:

Phoebe
"Hi, I am undefined"

Somehow I get to pass this exercise and move on to next but I think the output is wrong.
Can anyone tell me if this is the right answer?


#2

it should be the other way around, you want call nameString("Phoebe") which you want to log to the console


#3

Thank you! I have changed the code and its fine now.
But I have another question, since we can just call the function and it gives us the answer as well, why do we need to use 'console.log' ?


#4

well, in a way it doesn't. Run this code:

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

see how it only outputs the final function call? This is known as console behavior. The chrome console for example has the same behavior, it logs the last value by default.

to log both, we need console.log()


Having a problem with creating a new object
#5

I see! Thank you very much =)


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.