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 trial
Willie Suggs
Courses Plus Student 5,879 Pointshelp with understanding javascript functions
Challenge Task 2 of 2
After your newly created returnValue function, create a new variable named echo. Set the value of echo to be the results from calling the returnValue function. When you call the returnValue function, make sure to pass in any string you'd like for the parameter.
The function will accept the string you pass to it and return it back (using the return statement). Then your echo variable will be set with what is returned, the results.
function returnValue(car) {
return car;
}
return returnValue(mercedes);
var echo = "mercedes";
4 Answers
Kevin Korte
28,149 PointsHi Kendra,
There are some syntax issues which is what is giving you trouble. Let's start over from the beginning.
So it says "After your newly created returnValue function, create a new variable named echo"
function returnValue(car) {
return car;
}
var echo;
and than "Set the value of echo to be the results from calling the returnValue function." which means we'd have
function returnValue(car) {
return car;
}
var echo = returnValue();
and finally it says "When you call the returnValue function, make sure to pass in any string you'd like for the parameter"
so we have
function returnValue(car) {
return car;
}
var echo = returnValue("mercedes");
So now if you were to check the value of echo, you would find it it equal to the string of "mercedes".
Erik Nuber
20,629 Pointsfunction returnValue(car) {
return car;
}
var echo = returnValue("mercedes");
The first statement is the function. It is a way to separate your code to do something generally useful. In this particular case, it isn't that impressive but, it isn't the point. Between the parenthesis the word car is a parameter that can be used within the function. The parameter can be called anything you would like it to be. In this case, it is called car and, should be dealing with cars. Being a simple function, it is just returning the parameter car back out.
The return statement will end a function immediately. So any code after return will not run. This can be useful if you have an if/else statement where you want to leave the function if something is true else run the else statement and do something further afterwards.
Finally, the call to the function. Here we have created a variable called echo. We are immediately assigning it a value in this case, it is the value of what is returned by the function call. returnValue() calls the function and, as the function is expecting a parameter we are sending it an argument, in this case mercedes. If this was not sent in, echo would be undefined.
function returnValue(car) {
return car;
}
var echo = returnValue();
Here, if nothing were passed into the function, echo would hold nothing because nothing was sent into a function expecting a parameter and, the return car statement would also not be defined.
Finally, in the original function, when passing in a string like mercedes you need to remember the quote marks. This makes it a string. If mercedes were a variable holding a value than you wouldn't need the quote marks.
ex
function returnValue(car) {
return car;
}
var mercedes = "Mercedes C250"
var echo = returnValue(mercedes);
Here you no longer need quote marks because mercedes holds a value and is not a string.
Willie Suggs
Courses Plus Student 5,879 Pointsthanks, totally broke it down for me
Hussein Musse
3,024 PointsNice answer Erick nuber
Julian Gutierrez
19,325 PointsJulian Gutierrez
19,325 Points*edited markdown-JG