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
Ayaz Sadiq
Courses Plus Student 5,361 PointsPassing an argument into a function
Can someone please tell me what is wrong with my code. My code will not run.
function returnValue(food) { return food }
var echo { return food alert(returnValue); }
2 Answers
Cindy Lea
Courses Plus Student 6,497 PointsI believe your last statement is wrong. You need to store the value returned from the function in a variable then use echo to display it. Youre trying to call the function again in the echo.
Craig Watson
27,930 PointsHi,
best way to explain is with some code so below is a small example.
var checkFood = function(){
var favFood = prompt("What is your favourite food?").toLowerCase();
if ( favFood != "icecream" ) {
alert("Why didn't you choose icecream?");
} else {
alert("Mine to!");
}
};
checkFood();
First thing we do is create the variable to store the function in, inside that we then we create the prompt to get the value and store that in a separate variable. Now we have the result we can run the test to see if they like icecream just as much as I do and "alert" them to how I feel about their choice.
hope this helps craig