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 trialShahar Ohayon
4,868 PointsPassing an Argument to a Function
I don't know what the F@#$@#$ I'm doing wrong! T_T
function returnValue(argument) {
return argument;
returnValue('hello');
var echo = "returnValue('hello')";
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Benjamin Barslev Nielsen
18,958 PointsThe code inside the function is never executed, since you never call returnValue outside the function. You correctly call the function in line 3, but this code is not evaluated before you have called the function. Therefore the function call needs to go outside of the function. I will now assume that line 3 and 4 was written outside the function:
function returnValue(argument) {
return argument;
}
returnValue('hello');
var echo = "returnValue('hello')";
The desired result to be stored in echo is the result of returnValue('hello'), but right now you are storing the string "returnValue('hello')" in echo instead, i.e, in the new line 5 you do not make a function call, but only writes that exact string value to echo. The solution would therefore be:
function returnValue(argument) {
return argument;
}
var echo = returnValue('hello');
returnValue('hello') evaluates to 'hello' and 'hello' is then stored in echo.
Rafal Kita
5,496 PointsYes it is confusing at the beginng. Took me some time to understend as well and that's the way I passed it.
function returnValue(abc) {
return abc;
}
var echo = returnValue('alphabet');
Shahar Ohayon
4,868 PointsShahar Ohayon
4,868 PointsThank you! I got so mad lol