Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Cranny
3,019 PointsFunction to Variable
What is wrong with this code
function returnValue(anything) { return anything; }
var echo = returnValue();
returnValue("win");
2 Answers
0yzh
17,276 PointsHey Mark,
The reason the code is not working is because you did not pass through an argument when assigning the function call returnValue(); to the variable echo. This makes the value of echo undefined. You can try something like this:
var echo = returnValue('win');
Derek Gibson
6,531 PointsWhat are you trying to accomplish with this code? Technically nothing is wrong with this code.
/* ORIGINAL CODE */
function returnValue(anything) {
return anything;
}
var echo = returnValue();
returnValue("win");
Your code first creates a function named returnValue with a parameter named "anything" IF your function is called, the code inside the function will run, that code "return anything" will return a the value of whatever argument you pass to it.
Second, you created a variable named echo which is equaled to your returnValue function with NO argument passed to it.
Third, you call the function correctly passing "win" as a argument to it, which executes the code within the function, returning "win". If you changed the last line of your code to:
console.log( returnValue("win") );
and opened the console, you would see the string 'win' in the console.
I am still learning myself so I hope this answer helps and is factual. I don't understand the purpose of creating the echo variable in this code but like I previously mentioned I am also learning. Good luck!
Mark Cranny
3,019 PointsThanks, one of the tasks in the Javascript wasn't completing and throwing an error. Huy Bui was correct I had to assign the value as an argument at the time of assigning it to the variable like:
var echo = returnValue('win');
Thanks for the input
Mark Cranny
3,019 PointsMark Cranny
3,019 PointsThanks Huy Bui,
I got a little confused by the wording in the lesson.